This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.