Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions source/ch6_definingclasses.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,14 @@ public int compareTo(Fraction other) {
<code>
class Student:
numStudents = 0
def __init__(self, id, name):
def __init__(self, id, name):
self.id = id
self.name = name
Student.numStudents = Student.numStudents + 1
# this is a static variable, that can be accessed without the self prefix
Student.numStudents = Student.numStudents + 1
def main():
for i in range(10):
s = Student(i,"Student-"+str(i))
s = Student(i,"Student-"+str(i)) # create a new Student object
print('Number of students:', Student.numStudents)
main()
</code> <tests> </tests>
Expand All @@ -856,13 +857,13 @@ main()
<program xml:id="java-static" interactive="activecode" language="java">
<code>
public class Student {
public static Integer numStudents = 0;
public static Integer numStudents = 0; // static member variable, shared by all instances of the class
private int id;
private String name;
public Student(Integer id, String name) {
this.id = id;
this.name = name;
numStudents = numStudents + 1;
numStudents = numStudents + 1; // a static variable, that can be accessed without the Student prefix
}
public static void main(String[] args) {
for(Integer i = 0; i &lt; 10; i++) {
Expand Down