Unusual behavior of java program
I am new to java and trying to learn it. I wrote two class as follows, and
I expect to print as 1, 2, 3 but it prints 3, 3, 3. I read a java book
about i couldn't figure out the above behavior and print 1,2,3.
public class Student{
private static int indexNumber = 0;
Student(){
indexNumber=indexNumber+1;
}
public void test() {
System.out.println(this.getIndexNumber());
}
public int getIndexNumber(){
return indexNumber;
}}
public class College {
public static void main(String args[]){
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
student1.test();
student2.test();
student3.test();
}
}
Can anyone help?
No comments:
Post a Comment