Difference between creating an instance variable and creating a new object in Java?

后端 未结 6 1422
生来不讨喜
生来不讨喜 2020-12-10 21:36

I understand the difference between creating an object and creating a variable. For example:

private int number;
MyClass myObj = new MyClass();
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 21:58

    private MusicPlayer player;
    

    declares an instance variable named player but does not initialize it.

    player = new MusicPlayer();
    

    assigns a value to the already-declared field.

提交回复
热议问题