I understand the difference between creating an object and creating a variable. For example:
private int number;
MyClass myObj = new MyClass();
But my point here is what's the difference between these two?
private MusicPlayer player;
player = new MusicPlayer();
MusicPlayer is a class, but what exactly are we doing here?
private MusicPlayer player;
Here you create a reference variable of MusicPlayer class (but it does not create an object) without initializing it. So you cannot use this variable because it just doesn't point to anywhere (it is null).
For example, using a Point class:
Point originOne;
can be represented like this:

player = new MusicPlayer();
Here, you allocate an object of type MusicPlayer, and you store it in the player reference, so that you can use all the functions on it.
For example, using a Point class, with x and y coordinates:
Point originOne = new Point(23, 94);
can be represented like this:

The combination of the two lines is equivalent to:
private MusicPlayer player = new MusicPlayer();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With