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

后端 未结 6 1437
生来不讨喜
生来不讨喜 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 22:04

    private MusicPlayer player;
    

    It's a declaration, This means to make a new reference variable of MusicPlayer, just a reference, and no instance will be created. You cannot use it because it points to null.

    player  = new MusicPlayer();
    

    MusicPlayer() calls the MusicPlayer constructor, new create a instance of MusicPlayer, = assigns this instance to the reference player.

提交回复
热议问题