Skip to main content
added 4 characters in body
Source Link
Nikhil Agrawal
  • 26.7k
  • 21
  • 93
  • 127

Here is the explination why testList still have a "null" at first node after these piece of code

testList.add(obj1);//after this line of code, testList will have a "null" at first node obj1 = new MyClass();//after this line of code, testList still have a "null" //at first node... //What I want is that testList's first node will become a new //MyClass object 

Step 1

MyClass obj1 = null; 

This line creates space for the MyClass reference variable (the bit holder for a reference value), but doesn't create an actual Myclass object.

Step 2

List<MyClass> testList = new ArrayList<MyClass>(); 

A list is created testList which can hold objects of MyClass types

Step 3

testList.add(obj1);//after this line of code, testList will have a "null" at first node 

testList first node will now refer to an null But not an MyClass Object.

step 4step 4

obj1 = new MyClass(); 

Creates a new MyClass object on the heap and Assigns the newly created MyClass object to the reference variable ob1obj1.

So now how will the list gets updated it is still holding an null But not an MyClass Object.

So now if you want to make testList's first node to become a new MyClass object

TheThen write this lone of code after obj1 = new MyClass();

testList.set(0, obj1); 

So now the full code after that would be

MyClass obj1 = null; List<MyClass> testList = new ArrayList<MyClass>(); testList.add(obj1);//after this line of code, testList will have a "null" at first node obj1 = new MyClass(); testList.set(0, obj1); 

Thats all about your problem from my understanding.

Here is the explination why testList still have a "null" at first node after these piece of code

testList.add(obj1);//after this line of code, testList will have a "null" at first node obj1 = new MyClass();//after this line of code, testList still have a "null" //at first node... //What I want is that testList's first node will become a new //MyClass object 

Step 1

MyClass obj1 = null; 

This line creates space for the MyClass reference variable (the bit holder for a reference value), but doesn't create an actual Myclass object.

Step 2

List<MyClass> testList = new ArrayList<MyClass>(); 

A list is created testList which can hold objects of MyClass types

Step 3

testList.add(obj1);//after this line of code, testList will have a "null" at first node 

testList first node will now refer to an null But not an MyClass Object.

step 4

obj1 = new MyClass(); 

Creates a new MyClass object on the heap and Assigns the newly created MyClass object to the reference variable ob1.

So now how will the list gets updated it is still holding an null But not an MyClass Object.

So now if you want to make testList's first node to become a new MyClass object

The write this after obj1 = new MyClass();

testList.set(0, obj1); 

Here is the explination why testList still have a "null" at first node after these piece of code

testList.add(obj1);//after this line of code, testList will have a "null" at first node obj1 = new MyClass();//after this line of code, testList still have a "null" //at first node... //What I want is that testList's first node will become a new //MyClass object 

Step 1

MyClass obj1 = null; 

This line creates space for the MyClass reference variable (the bit holder for a reference value), but doesn't create an actual Myclass object.

Step 2

List<MyClass> testList = new ArrayList<MyClass>(); 

A list is created testList which can hold objects of MyClass types

Step 3

testList.add(obj1);//after this line of code, testList will have a "null" at first node 

testList first node will now refer to an null But not an MyClass Object.

step 4

obj1 = new MyClass(); 

Creates a new MyClass object on the heap and Assigns the newly created MyClass object to the reference variable obj1.

So now how will the list gets updated it is still holding an null But not an MyClass Object.

So now if you want to make testList's first node to become a new MyClass object

Then write this lone of code after obj1 = new MyClass();

testList.set(0, obj1); 

So now the full code after that would be

MyClass obj1 = null; List<MyClass> testList = new ArrayList<MyClass>(); testList.add(obj1);//after this line of code, testList will have a "null" at first node obj1 = new MyClass(); testList.set(0, obj1); 

Thats all about your problem from my understanding.

Source Link
Nikhil Agrawal
  • 26.7k
  • 21
  • 93
  • 127

Here is the explination why testList still have a "null" at first node after these piece of code

testList.add(obj1);//after this line of code, testList will have a "null" at first node obj1 = new MyClass();//after this line of code, testList still have a "null" //at first node... //What I want is that testList's first node will become a new //MyClass object 

Step 1

MyClass obj1 = null; 

This line creates space for the MyClass reference variable (the bit holder for a reference value), but doesn't create an actual Myclass object.

Step 2

List<MyClass> testList = new ArrayList<MyClass>(); 

A list is created testList which can hold objects of MyClass types

Step 3

testList.add(obj1);//after this line of code, testList will have a "null" at first node 

testList first node will now refer to an null But not an MyClass Object.

step 4

obj1 = new MyClass(); 

Creates a new MyClass object on the heap and Assigns the newly created MyClass object to the reference variable ob1.

So now how will the list gets updated it is still holding an null But not an MyClass Object.

So now if you want to make testList's first node to become a new MyClass object

The write this after obj1 = new MyClass();

testList.set(0, obj1);