Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access the instancing class

Status
Not open for further replies.

MartinF

Technical User
Sep 19, 2000
143
FR
Hi there,

Kinda new to Java, and trying to reference the class that created an instance of the class i am referencing from.

i.e.
If Class A creates an instance of Class B, how can i write code in Class B that accesses a member (a variable in this case) of Class A. The instance of Class B was created under the default constructor public B(){...}

I did try searching the forum but wasnt quite sure what to search for. Im sure this must be very simple.

Thanks in advance
 
It's not that simple because that's totally against the OO design principles.

If you want to access A from B, the reference to A should be passed in the B constructor.

You can use reflection but I wouldn't. If you're new, I'd encourage you to do things like they should be.

Cheers,
Dian
 
Thanks for the feedback Dian,

This was the original way i was going to do it, but was wondereing if there was a shortcut method i didnt know about yet.

Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top