File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -849,13 +849,14 @@ public int compareTo(Fraction other) { // compare this fraction with another fra
849849 <code >
850850class 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
856857def 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)
860861main()
861862 </code > <tests > </tests >
@@ -870,13 +871,13 @@ main()
870871 <program interactive =" activecode" language =" java" >
871872 <code >
872873public 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 < 10; i++) {
You can’t perform that action at this time.
0 commit comments