This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Accessing varaibles from another class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing varaibles from another class." Watch "Accessing varaibles from another class." New topic

Accessing varaibles from another class.

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Suppose that there are 2 separate classes. Is it possible to access some variable that is in a private method of the other class? If so could you please provide me an example of how this could be done?
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Naf,
You have a couple of problems.
Things are declared private so people cannot access it from any other classes.(not sure about extending though)
Method variables are stored on stack only while that method is running.

The nice thing about Standards is that there are so many to choose from!
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Generally speaking... "No you can't". Private methods can't be accessed from any other class, except from the class they are located in (Even if they were subclassed).

However you should know that in the case of inner classes it will be a different story, see code:

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As stated, the answer to your question is "no." However, if you describe what you want to do in more detail (perhaps with some code to illustrate), we may be able to suggest a solution that uses the tools that Java provides.

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Accessing varaibles from another class.
It's not a secret anymore!