File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -836,13 +836,13 @@ public int compareTo(Fraction other) {
836836 <code >
837837class Student:
838838 numStudents = 0
839- def __init__(self, id, name):
839+ def __init__(self, id, name):
840840 self.id = id
841841 self.name = name
842- Student.numStudents = Student.numStudents + 1
842+ Student.numStudents = Student.numStudents + 1 # increment the static variable
843843def main():
844844 for i in range(10):
845- s = Student(i,"Student-"+str(i))
845+ s = Student(i,"Student-"+str(i)) // create a new student
846846 print('Number of students:', Student.numStudents)
847847main()
848848 </code > <tests > </tests >
@@ -856,13 +856,13 @@ main()
856856 <program xml : id =" java-static" interactive =" activecode" language =" java" >
857857 <code >
858858public class Student {
859- public static Integer numStudents = 0;
859+ public static Integer numStudents = 0; // static member variable, shared by all instances of the class
860860 private int id;
861861 private String name;
862862 public Student(Integer id, String name) {
863863 this.id = id;
864864 this.name = name;
865- numStudents = numStudents + 1;
865+ numStudents = numStudents + 1; // a static variable, that can be accessed without the Student prefix
866866 }
867867 public static void main(String[] args) {
868868 for(Integer i = 0; i < 10; i++) {
You can’t perform that action at this time.
0 commit comments