• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

referencing an object

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just toying with reference variables etc


how would i get sportsCar reference variable to point to where it was originally pointing to?
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry full code here



obviously i tried sportsCar = Sportscar but i knew that wouldnt do it as it is already pointing to fourByFour,so its almost like a duplicate
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With this code, there is no way to get sportsCar to point to its original object, because that object's location is no longer known to the code.

You would have to make a significant change. You would have to keep track of the original object like this:


Now sportsCar points at its original object.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jim...so basically you pointing it to another (temporary) variable...that then can be pointed back...makes sense
 
Jim Ryan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of sportsCar as a pointer, as a finger pointing at a vehicle. You want to point it at something else for a while, so you create another finger, "tempReference," and point it at that vehicle. Now you have two fingers pointing at the same vehicle. Now you are free to point sportsCar at something else for a while. Later, you can come back and point sportsCar at what tempReference has been patiently pointing at while waiting for sportsCar to return. Now you have the two fingers pointing at the same vehicle again. At this point tempReference is no longer needed.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would you typically call it tempreference? or could ou call it originalSportsCar ...or it doesnt really matter..just trying to stick to convention
thanks for the help...
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:would you typically call it tempreference?



I'd be unlikely to write that code, so I wouldn't have to make that decision. If I had to write some code which changed a variable to refer to object X and which then changed it back to refer to the original object -- which is very unlikely -- I would decide then. Made-up examples which don't bear much relationship to reality aren't good tools for learning that sort of thing.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Paul. Why exactly would you need such a scenario ?

Here is a equivalent question to what you are asking without the use of OOP :

The answer is simple, if you want a reference/value, you keep it.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys

i m self studying and dont work in the industry...i was watching a tutorial that explained these concepts
it used a reference to one object and then another
i had no idea that you would never have to point a reference variable back to the original object,i was just wondering if this could be done...but after what you guys are saying, it doesnt seem necessary...
thanks
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jon,
In languages such as C, C++ the programmer is responsible to allocate memory, access them using pointers and then deallocate memory.

In Java, an object can be pointed to by using a reference. If an object does not have any references pointing to it, its eligible to be "deallocated" or garbage collected. Hence, we loose the reference to it. This frees the programmer from worrying about memory allocation and leaves it to java.

On a side note, it's cool to know that you are self studying Java. I would suggest that you start writing small programs to help you understand java better.
You could start with simple "Guess a number" program, then increase complexity as you go further. You could soon catchup with the industry guys ;)
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:i had no idea that you would never have to point a reference variable back to the original object,i was just wondering if this could be done...but after what you guys are saying, it doesnt seem necessary...



I'm not saying "never"... but the example you posted wasn't all that realistic. And in Java it's the objects which are the things you need to work with. The variables are just tools which help you access the objects. So typically you set a variable to refer to an object and then you work with the object. Then using that variable to refer to another object and then using it to refer to the first object again, that just seems like why wouldn't you just use two variables? But my imagination isn't infinite so there certainly could be use cases for that.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys

yes written the guess the number game

written a few programs now

thanks for the encouragement

 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic