0

Now, when I create a new object in Java from a particular class and assign it to a variable what do we call that variable?

Class1 new1 = new Class1();

The question is about "new1" if it it's an object or a variable carrying an object. Also, when we say,

public class Class1 {
Class2 n1;
}
public class Class2{
void method();
}

Is n1 considered to be an instance variable or an object of another class?

If it's an object what it's carrying before writing,

n1 = new Class2();

?

svick
  • 236,525
  • 50
  • 385
  • 514
Badr
  • 694
  • 1
  • 7
  • 26

2 Answers2

0
  • Question 1: It is called a reference. new1 holds a memory address1, where the new created object of type Class1 is located.

  • Question 2: Normally, you call it an attribute or a field. The fact that it is an object rather than a primitive does not matter here.

  • Question 3: an uninitialized reference is null, if it is a class attribute. If the reference, however, is a local variable, it is uninitialized, as EJP pointed out. You cannot read an uninitialized local variable, this will result in a compilation error.

One further remark: In Java, objects are always stored within the heap memory. No exception. Your local variables, however (like new1 in the first example) are located on the stack and references to the new object on the heap. This is the reason why Java is pass-by-value.


1 Actually, not the memory address, but an unique id is referenced. Java hides physical memory addresses.

Community
  • 1
  • 1
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 2
    _an uninitialized object is null_. No, an unitialized **variable**. – Sotirios Delimanolis Jul 03 '15 at 23:18
  • An uninitialized reference is *uninitialized.* The *default initial value* for references that are *members of a class* is null. It isn't the same thing. In this case `new1` is a local variable, as you said yourself, and has no initial value at all. And your final paragraph is *not* 'the reason why Java is pass-by-value'. The reason for that is simply that it lacks reference semantics for method arguments, unlike say C++ or Pascal. – user207421 Jul 04 '15 at 00:49
0

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216