AJD Ajay Dubey AJD Ajay Dubey -4 years ago 94
Java Question

what's the difference between these 2 ways of declaring objects in java?

Display dis;


Display dis = new Display();

what exactly these two statements do?
I've tried them both but can't seem to know the difference.
P.S. noob coder here

Answer Source

Unlike other programming languages like C++, it is not sufficient to declare a variable to have an actual object assigned to it:

Display dis; // dis == null

You need to instantiate an object with

dis = new Display(); // dis now contains a reference to a new Display instance

You can declare a variable and assign a reference to a new instance at the same time, if you like:

Display dis = new Display();

(Note: in Java it is customary to start class names with an uppercase letter)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download