Java class variable that tracks number of objects instantiated

前端 未结 3 1718
长情又很酷
长情又很酷 2020-12-21 08:09

I have this class, Student, with the variable StudentID:

public class Student extends Person{
  int studentID = 0;
  int level;

  public Student(){

  }         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-21 09:03

    Add a static counter and initialize studentID with it, incrementing it in the process :

    public class Student extends Person{
      static counter = 1;
      int studentID = counter++;
      ...
    

提交回复
热议问题