Difference between initializing a class and instantiating an object?
I tried searching for this question through the search engine but could find a topic that explained the difference between initializing a class and instantiating an object.
Could someone explain how they differ?
There are three pieces of terminology associated with this topic: declaration, initialization and instantiation.
Working from the back to front.
Instantiation
This is when memory is allocated for an object. This is what the new
keyword is doing. A reference to the object that was created is returned from the new
keyword.
Initialization
This is when values are put into the memory that was allocated. This is what the Constructor of a class does when using the new
keyword.
A variable must also be initialized by having the reference to some object in memory passed to it.
Declaration
This is when you state to the program that there will be an object of a certain type existing and what the name of that object will be.
Example of Initialization and Instantiation on the same line
SomeClass s; // Declaration
s = new SomeClass(); // Instantiates and initializes the memory and initializes the variable 's'
Example of Initialization of a variable on a different line to memory
void someFunction(SomeClass other) {
SomeClass s; // Declaration
s = other; // Initializes the variable 's' but memory for variable other was set somewhere else
}
I would also highly recommend reading this article on the nature of how Java handles passing variables.
When a Java class is "loaded" into the JVM the class representation must be initialized in several ways.
- The class's "constant pool" is expanded into a runtime structure and some values in it are initialized.
- The superclass of the class is located (via the constant pool) and attributes of it extracted.
- A method table is constructed for the methods of the class. The individual methods are marked as "not yet verified".
- Several verification operations are performed on the class representation.
- Static fields are initialized.
- On first reference, string literals are "interned" and the interned string pointer is placed in the constant pool
- On first reference methods are "verified".
- Et al.
There is a specific set of terminology used to refer to class initialization, though I don't recall the specifics. Certain things can only occur after a class has been initialized to a specific point, etc.
Instantiating an object can only occur after the class has been loaded and initialized (though all methods do not need to have been verified). The size of the object is gotten from the class and that much heap is located and zeroed. The object header is filled in with a pointer to the class and other fields used to manage the class. Then the appropriate constructor method for the class is invoked (and it will invoke any super's constructor).
Point originOne = new Point(23, 94); -- Example 1
Rectangle rectOne = new Rectangle(originOne, 100, 200); -- Example 2
1) Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
2) Instantiation: The new keyword is a Java operator that creates the object.
3) Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
Reference:https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html#:~:text=Instantiation%3A%20The%20new%20keyword%20is,which%20initializes%20the%20new%20object.