Instantiating a reference type
suggest changeObject obj = new Object(); // Note the 'new' keyword
Where:
Objectis a reference type.objis the variable in which to store the new reference.Object()is the call to a constructor ofObject.
What happens:
- Space in memory is allocated for the object.
- The constructor
Object()is called to initialize that memory space. - A reference to that object is stored in
obj. The reference is not the object’s fields, and it is not required to be a raw memory address.
This is different from primitives:
int i = 10;
Where the actual value 10 is stored in i.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents