2

I've been practicing component based design pattern and I was wondering if when you initialize an variable without reference meaning initialized as null, Java go ahead and attribute a space in memory that has the size of the variable even though it is set to null so that eventually when you need to reinitialize it with a new instance of a class it just copies the fields of the new instance?

Falla Coulibaly
  • 759
  • 2
  • 11
  • 23
  • You still seem a bit confused on the difference between object and variable. Understand that they exist as completely separate entities, that objects can have a thousand variables refer to the same one instance, and that objects can have no variables directly referring to them. – Hovercraft Full Of Eels Mar 09 '16 at 03:26

3 Answers3

3

A variable whose type is a reference type occupies the same amount of space whether it contains null or a reference to an object.

However, the variable only holds the reference ... not the object itself.

... when you need to reinitialize it with a new instance of a class it just copies the fields of the new instance?

Erm ... no. When you later "initialize" the variable, you are assigning a reference to the variable. You are not copying the fields of the object.

For example:

  SomeType s = null;     // the declaration sets aside space for one
                         // reference, and the initialization assigns
                         // `null` to it.

  s = new SomeType(...)  // the 'new' expression creates the object and 
                         // which allocates the space, etcetera
                         // the assignment merely assigns the reference
                         // for that object to 's'.

What if "s" is an array of "Sometype" instead still initialized to null, will it be legit to assume that only space for one reference will be saved until you create a new valid reference for an array of the relevant type?

An array type is also a reference type. So, yes, the answer is the same. A declaration SomeType[] s would reserve space for one reference.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for your answer,it makes much more sense this way. In your example, you wrote " the declaration sets aside space for one reference..." so the null then null means there is no reference to look for. What if "s" is an array of "Sometype" instead still initialized to null, will it be legit to assume that only space for one reference will be saved until you create a new valid reference for an array of the relevant type? – Falla Coulibaly Mar 09 '16 at 03:50
1

I was wondering if when you initialize an variable without reference meaning initialized as null, Java go ahead and attribute a space in memory that has the size of the variable even though it is set to null

Yes memory is allocated for the variable, but this is only a tiny address space bit of memory and nothing else. No memory is allocated for the eventual object.

so that eventually when you need to reinitialize it with a new instance of a class it just copies the fields of the new instance?

When you create an instance of anything, then memory is allocated on the heap for the object, and this happens whether or not the object is assigned to a variable, to no variables, or to 50 variables, and any variable that refers to the object has its address space pointing at the object's location on the heap (perhaps -- I don't think that the actual mechanics, the hows, are fully specified)

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

Have a look at oracle documentation page regarding objectcreation

Point originOne;

If you declare originOne like this, its value will be undetermined until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.

For that, you need to use the new operator, as described in the next section. You must assign an object to originOne before you use it in your code.

enter image description here

Instantiating a Class

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like:

Point originOne = new Point(23, 94);

enter image description here

I hope above picture clarifies your queries.

The size of reference will be 4 bytes or 8 bytes. Have a look at this SE question:

How big is an object reference in Java and precisely what information does it contain?

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211