• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variable accessibility

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic