32

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?

arnold_palmer
  • 525
  • 2
  • 5
  • 6
  • 6
    There is no such thing as initializing a class. Do you mean initializing a variable? – Femaref Feb 25 '13 at 18:45
  • do you mean **initialize an Object** ?? – PermGenError Feb 25 '13 at 18:45
  • @Femaref, perhaps the term is not technical, but I think it's fair to describe static initializers and constructors as initializing a class and an instance respectively. – Kirk Woll Feb 25 '13 at 18:46
  • That's stretching it very far though. – Femaref Feb 25 '13 at 18:47
  • Read this--> http://stackoverflow.com/questions/2330767/what-is-the-difference-between-instantiated-and-initialized – Java42 Feb 25 '13 at 18:47
  • 3
    @Femaref - (And upvoters) There definitely is something that corresponds to initializing a class, though I don't recall if that's the "legal" term. – Hot Licks Feb 25 '13 at 18:48
  • I don't disagree that there is a certain concept that might fit the description, but I don't agree with the term itself. – Femaref Feb 25 '13 at 18:59
  • 3
    JLS 8.3.1.1 "A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4)." What term do you disagree with?? – Hot Licks Feb 25 '13 at 19:01
  • 2
    8.7 "A static initializer declared in a class is executed when the class is initialized (§12.4.2)." – Hot Licks Feb 25 '13 at 19:03
  • 5
    (I think you folks owe the OP an apology ... and undoing the downvotes on his question, since it obviously was not "common knowledge".) – Hot Licks Feb 25 '13 at 19:04
  • Yes! I'm getting down voted and I had to no idea that these are the wrong terms or are they? I'm very confused at the moment :( – arnold_palmer Feb 25 '13 at 19:12
  • 5
    The thing is, you used a perfectly legitimate term, but some idiots thought it was the wrong term and jumped on you. Welcome to SO! – Hot Licks Feb 25 '13 at 19:18
  • Sure seems that way. Thanks for your explanations! – arnold_palmer Feb 25 '13 at 19:27

4 Answers4

47

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.

lachy
  • 1,833
  • 3
  • 18
  • 27
  • 1
    Could you give an example where initialization and instantiation do _not_ happen at the same time? – Leo Mar 28 '17 at 16:57
  • @Leo I updated the post... let me know if that helped! – lachy Mar 31 '17 at 06:42
  • where **"s"** is getting instantiated(when does its memory gets allocated) ? –  Jun 13 '17 at 05:38
  • When the 'new' keyword is called. https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html – lachy Jun 25 '17 at 00:15
  • 2
    This is not the right answer. Class initialization does exist. Look at method Class.forName(String). A call to Class.forName("X") causes the class named X to be initialized. https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String) – Alex Mar 13 '18 at 18:31
  • @Alex I am not sure what you're talking about? I don't say that Class initialisation doesn't exist... I can update the answer to include how instantiation and initialisation work for reflection if that is what you're looking for but I feel that is heading off topic. – lachy Mar 27 '18 at 00:11
  • 1
    @lachy, Hi, maybe arnold_palmer did not know what he asked. He asked about Class initialization but maybe he just meant initializing a variable. In this case, your answer is perfect and very detailed. [This is why I didn't downgrade your answer]. You explained very well the difference between initializing a variable, instantiating an object and declaring a variable. However Class initialization is something different. It loads the class into memory (the methods, initiates the static fields) and instantiates an object of type Class. Yes, probably arnold_palmer meant "initialize a variable" – Alex Apr 09 '18 at 23:19
  • 1
    @Alex One should answer the question that was asked. The OP's own comments show that you are dead flat wrong about this. – user207421 Apr 20 '19 at 23:47
  • 1
    NIce explanation – Isabel Jinson Mar 19 '22 at 19:47
4

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).

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • 1
    This is the right answer. Class initialization does exit. See Class.forName method at https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String) – Alex Mar 13 '18 at 18:24
4

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.

0

Initializing a class is done using a static initialization block. (static { }). It's not a method, it's an initializer. It is executed the first time the class itself is referenced.

Instantiating an object is done for example with new keyword by calling its constructor. At that time static initialization block will not be executed.

Asi Mugrabi
  • 261
  • 2
  • 10