aspose file tools*
The moose likes Beginning Java and the fly likes variable accessibility 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 "variable accessibility" Watch "variable accessibility" New topic
Author

variable accessibility

Krithika Beri
Ranch Hand

Joined: Sep 11, 2012
Posts: 36
I have a variable in one class and i want to be able to edit that variable in another class such that when i make changes in class2 is should be updated in class1. Please help.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The brute force way to do this is to simply provide a setter method:


However, it's not a good idea to just go around blindly providing that kind of direct access without a good reason. Sometimes it makes sense to do so, but often times it's better design to just give C1 real behavior through methods, and let those methods update C1's member variables as appropriate. As much as it practical, the details of C1's state should stay hidden from users of C1.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
You should not have the same value in two classes. You should have the value in one class and one only. It should have one copy and one only. If you need the other class to use it, it can be provided by a public getXXX method. If you really need to signal the other object about a change, you are getting into more advanced practices: look for the observer pattern.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable accessibility