When I create a new object in Java from a particular class and assign it to a variable what do we call that variable?
In the general case, we just call it a variable.
We could also call it an initialized variable. But "initialized" is just an adjective that reflects the fact that you have assigned something to it. There are other adjectives that could apply; e.g. "instance variable", "local variable", "class variable", and there are common synonyms for some of these.
The question is about "new1" if it it's an object or a variable carrying an object.
It is definitely NOT an object.
Is n1
considered to be an instance variable or an object of another class?
It is an "instance variable". It is also known as a "field" or an "attribute", but "instance variable" is the terminology that the Java Language Specification uses.
If it's an object what it's carrying before writing n1 = new Class2();
It isn't an object. A variable isn't an object, or a primitive value. A variable will hold a value, but it isn't a value.
(The analogy often used is that a variable is a "slot" or "pigeonhole" that can hold something. The variable is the slot, not the thing that is in the slot.)
The state of the variable n1
will be null
if it is an instance variable. If n1
is a local variable, the Java language won't let you see what the state is ... because it is illegal to access a local variable before it has been explicitly initialized.