Skip to content

Commit 18ce6ad

Browse files
authored
Merge pull request #323 from habibasorour/issue_315_listing
added comments to code in 6.6
2 parents 9c921e0 + 7002f82 commit 18ce6ad

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

source/ch6_definingclasses.ptx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -849,13 +849,14 @@ public int compareTo(Fraction other) { // compare this fraction with another fra
849849
<code>
850850
class Student:
851851
numStudents = 0
852-
def __init__(self, id, name):
852+
def __init__(self, id, name):
853853
self.id = id
854854
self.name = name
855-
Student.numStudents = Student.numStudents + 1
855+
# this is a static variable, that can be accessed without the self prefix
856+
Student.numStudents = Student.numStudents + 1
856857
def main():
857858
for i in range(10):
858-
s = Student(i,"Student-"+str(i))
859+
s = Student(i,"Student-"+str(i)) # create a new Student object
859860
print('Number of students:', Student.numStudents)
860861
main()
861862
</code> <tests> </tests>
@@ -870,13 +871,13 @@ main()
870871
<program interactive="activecode" language="java">
871872
<code>
872873
public class Student {
873-
public static Integer numStudents = 0;
874+
public static Integer numStudents = 0; // static member variable, shared by all instances of the class
874875
private int id;
875876
private String name;
876877
public Student(Integer id, String name) {
877878
this.id = id;
878879
this.name = name;
879-
numStudents = numStudents + 1;
880+
numStudents = numStudents + 1; // a static variable, that can be accessed without the Student prefix
880881
}
881882
public static void main(String[] args) {
882883
for(Integer i = 0; i &lt; 10; i++) {

0 commit comments

Comments
 (0)