0

I have a rather confusing class, in which I don't understand why superUser is always being printed. I know that if I would have written private String username = "user", then I would have been able to call it in my main method like that: System.out.println(o1.username). In my case the above would also print out superUser, because it is static. But I don't really get it.. is it because Java reads the code completely first and realizes that I have a second object o2, which has its own constructor, where the argument is assigned to the local variable username?

What I don't get is why System.out.println(o1.getUsername()); prints superUser.

public class PrintMe {

   private static String username = "user";
   private int password = 1234;
   public PrintMe(){}
   public PrintMe(String username){
       PrintMe.username = username;
       this.password = 5678;
   }

   public void changePassword(int password){
       System.out.println("The old password of " + this.getUsername() +
            " was " + this.password);
       this.password = password;
   }

   public String getUsername(){
       return PrintMe.username;
   }

   public static void main(String[] args){
       PrintMe o1 = new PrintMe();
       PrintMe o2 = new PrintMe("superUser");
       System.out.println(o1.getUsername()); // superUser
       System.out.println(o1.password); // 1234
       System.out.println(o2.getUsername()); // superUser
       System.out.println(o2.password); // 5678
       o1.changePassword(9000); // The old password of superUser was 1234
       System.out.println(o1.getUsername()); // superUser
       System.out.println(o1.password); // 9000
       System.out.println(o2.getUsername()); // superUser
       System.out.println(o2.password); // 5678
   }
}
NewbieJava
  • 117
  • 11
  • 1
    Have you visited [this](https://stackoverflow.com/questions/32417953/how-does-the-static-keyword-work-in-java)? BTW, Java is not difficult ;) – vinS Dec 16 '17 at 10:27
  • Nothing about your output is unexpected. Can you direct us to the exact line(s) which are confusing you? – Tim Biegeleisen Dec 16 '17 at 10:29

2 Answers2

1

Your username is static. This means that there is ever only one instance of that variable, no matter how many instances of PrintMe you have. The variable username is shared between all instances of PrintMe. This means that here:

   PrintMe o1 = new PrintMe();
   PrintMe o2 = new PrintMe("superUser");

The second line overwrites the username variable to be superUser. Since calling getUsername on either o1 or o2 returns the same variable, you get superUser as output.

To fix this, simply remove the word static from the variable declaration:

private String username = "user";
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

Since username is static it is not a property of the instance (o1, o2) but of the class. When the constructor PrintMe(String) is used the static username is modified. So when o2 is created the username is changed to "superUser". If you print o1.getUsername() before creating o2 you would get "user", afterwards you get "superUser". Remove the static modifier for the username and you will get the expected behavior.

Stefan
  • 4,645
  • 1
  • 19
  • 35