CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Mina Nabil
Level 17
Sydney
  • 05.03.2021
  • 343views
  • 6comments

Quick question please answer

Question about the task Fix four mistakes
Java Core,  Level 4,  Lesson 8
Under discussion
0
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Gellert Varga
Level 23 , Szekesfehervar, Hungary
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
Level 23 , Szekesfehervar, Hungary
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
Level 37 , Tampa, United States
5 March 2021, 20:04
This line sets the object of the variable 'list' to null
List<Number> list = null;
This line passes the object of 'list' to the initList() method
initList(list);
This line captures that object that is passed in and gives it a variable names 'list'
private static void initList(List<Number> list) {
This line takes the 'list' variable that is local to the block of code and assigns it a new object
list = new LinkedList<>();
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
printListValues(list);
and it passes the still null value of list that was set just a couple of lines prior
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
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
Level 17 , Sydney, Australia
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
Level 37 , Tampa, United States
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:
list = new LinkedList<>();
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
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.