GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes access to another classs' elements(variables)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "access to another classs Watch "access to another classs New topic
Author

access to another classs' elements(variables)?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
HI there,
I want to have access to a variable which is declared in another class.
e.g. I have class A and class B. I have a HashMap X in class A and I need to have access to this map from class B. for example I want to call X.remove() from inside class B.
how can I do this?
I have tried to declare my HashMAp public in class A and then import class A in class B , but still I dont have access to my X hashmap.
any suggestion would be greatly appreciated.

thanks,
sahar
david j greenfield
Greenhorn

Joined: Jan 11, 2010
Posts: 3
Please see below code snippet, i would suggest using encapsulation, its always good practice.

Here is my first class test, It has a TextField variable which is set to private (you could set it to public and access it directly, but that wouldn't be good OO design). I created a public method setTextField(String str), which gives access to the setText method of the TextField.




Here is my second class test2, who will access the class tests public method TextField, and pass a string into the setTextField method. Which will update the TextField.



Hope this helps, if its not totally clear please let me know. Also, it may help if you posted a snippet of what youre working on, or an example.

~ea
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

energy alchemist wrote:

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear energy alchemist,
That was so clear! I completely understand!

Thankyou so much!!!

Sahar.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Why have you declared that method and the field static?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access to another classs' elements(variables)?