aspose file tools*
The moose likes Beginning Java and the fly likes Converting between objects, does it retain anything in memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting between objects, does it retain anything in memory?" Watch "Converting between objects, does it retain anything in memory?" New topic
Author

Converting between objects, does it retain anything in memory?

Caden Whitaker
Greenhorn

Joined: Jan 15, 2009
Posts: 24
Standard User


Super Secret User



Main application


Ok... so that's a round about way of saing, if I return a type of "User" is it true in all circumnstances that any orphaned variables still remain somewhere attached to that object? Somewhere in memory perhaps? In my example, in main(), is it possible, somehow, somewhere, someway to determine jimmy's password just from examining the User object that was returned?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3652
    
  15
Your getUser method creates a SecretUser instance. This will always be a SecretUser instance - you cannot convert it to another type.

You are referencing it with a User object reference ('user'). This means that (without any casting) you can only access the User specific parts of the instance. However, there is nothing to stop you referencing it with a SecretUser reference and then all parts of the instance will become asccessible.


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11408
    
  16

The head first books explain casting pretty good. your object is like a tv, and your reference variable is like a remote.

so, you create a SecretUser object. That's like a TV that has picture-in-picture (PiP). you decide to connect it to a User reference - that's like a remote that doesn't have a button for PiP. The TV is still capable of doing it, you just don't have a button to turn it on.

Anyone else can get a remote that DOES have the PiP buttons, and if they use it, the PiP will come on.

A cast doesn't change the OBJECT, it only let's you connect a more or less 'sophisticated' reference to that object.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Caden Whitaker
Greenhorn

Joined: Jan 15, 2009
Posts: 24
Ahhh yeah, I should have known that!

Thanks!
Aurelian Tutuianu
Ranch Hand

Joined: May 13, 2004
Posts: 86
And, of course, there is also the reflection solution.



http://javasign.blogspot.com/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Converting between objects, does it retain anything in memory?