• Post Reply Bookmark Topic Watch Topic
  • New Topic

References  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys so I'm confused as to why this code does not work I'm under the impression that you can only have one reference with a certain name lets say stu in this example now creating a second reference with name stu will cause an error and rightfully so but what I don't get is how come we can create multiple references with the same name in a for loop




 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and a side question why do I get an error saying stu may not have been initialised when I clearly initialise stu inside the for loop?

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my second example in the for loop

is 10 objects been created and each time the loop is run stu is pointing to that new object?

or are ten objects been created and have the same name?

but the thing that baffles me the most is my last example
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:Hi guys so I'm confused as to why this code does not work I'm under the impression that you can only have one reference with a certain name lets say stu in this example now creating a second reference with name stu will cause an error and rightfully so but what I don't get is how come we can create multiple references with the same name in a for loop



This has nothing to do with references.  It has to do with declaration.  You can't declare the same variable twice.


You are creating ten different references, one by one, and they all go out of scope each iteration.  The for loop just means this to the compiler:
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:and a side question why do I get an error saying stu may not have been initialised when I clearly initialise stu inside the for loop?


Remember that the compiler is not an interpreter.  I'm guessing that it sees a declaration, then a loop which may be executed zero times, then the use of the variable which may or may not have been set.  (If the compiler were an interpreter, it would work much slower.)
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:in my second example in the for loop

is 10 objects been created and each time the loop is run stu is pointing to that new object?

or are ten objects been created and have the same name?

but the thing that baffles me the most is my last example

The variable stu gets set to a new student object ten times.  After the for loop, only the last student object is available.  The rest are set for garbage collection.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Knute
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!