Quick question please answer
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
5 March 2021, 22:54
Version-1.
main( ) {
List<Number> list = null;
initList(list);
printListValues(list);
}
Line2 . We don't have any list-object yet! We just assigned a null-value to a declared variable.
Line3. You send this null-value to the initList() method.
InitList method do these:
- gets this 'null',
- creates a local List<Number>list -variable. It's not the same as the List<Number>list -variable in the main()!
- creates a new List object, and assigns it to the local List<Number>list-variable,
- fills it up with elements.
- when method ends, the local variable terminates its own life. At this moment you loose the connenction to the created List object, so it will go to the garbage. The method is void = not returns anything, so when the program goes back to the main(), the List<Number> list variable of the main() method is still has the value 'null'.
Line4: the variable 'list' has the value "null".
Vers.2.
main() {
List<Number> list = new LinkedList<>();
initList(list);
printListValues(list);
}
Line2 . You creates a new list-object, and you assign its reference to the List<Number>list variable.
Line3. You send this reference to the initList() method.
InitList method do these:
- gets this reference,
- creates a local List<Number>list -variable. It's not the same as the List<Number>list variable in the main()!
- the received object-reference will be assigned to the local List<Number>list-variable,
- with the reference, it fills up the existing object with elements.
- when method ends, the local variable terminates its own life. But the filled up object not! The object already existed earlier, too. It has a variable in the main() method pointed to it, so the object's life won't terminate.
The method is void = not returns anything, but it's no problem: When the program goes back to the main() from the initList(), there the List<Number>list variable is pointing to the filled up object.
+1
Gellert Varga
5 March 2021, 22:17
In other words, create the list object already in main(), not in the void initList() method.
Do not assign a 'null'-value to the List <Number> list variable in the main() method, but a new LinkedList object.
This way you won't loose the changed list-object when the program goes back to main() from initList().
+1
Guadalupe Gagnon
5 March 2021, 20:04
This line sets the object of the variable 'list' to null
This line passes the object of 'list' to the initList() method
This line captures that object that is passed in and gives it a variable names 'list'
This line takes the 'list' variable that is local to the block of code and assigns it a new object
When the block of code ends, nothing is done with that object and it goes out of scope and will be removed next garbage collection
Meanwhile the code moves to this line
and it passes the still null value of list that was set just a couple of lines prior +1
Guadalupe Gagnon
5 March 2021, 20:06
So:
Main.list = null;
null is passed to initList() and assigned the variable name initList.list
initList.list gets a new object (Main.list does not get one)
+1
Mina Nabil
5 March 2021, 22:06
Sorry Guadalupe, I did not catch that well.
public static void main(String[] args) {
List<Number> list = null;
initList(list);
printListValues(list);
processCastObjects(list);
aren't these lines executed 1 by 1 after each other. So, for example list is assigned first to null and then the initList(list) assign it to new LinkedList then the same object named list is printed by printListValues . I am not gertting it why there are 2 list objects if you could clarify or even point me to an article, thanks so much.
0
Guadalupe Gagnon
5 March 2021, 22:21
initList() doesnt do anything to the list declared above it. It is initially passed to the method, but is immediately tossed out with the line:
This is because the 'list' variable in main is not the same as the list variable in initList(). Initially it is passed the object, which is null, but that first line disregards it and sets the list in initList() to a new object. Variables and objects are not the same thing.
+1