• 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

Accessing variables from other classes

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the difference if I create a get method in a class for a variable versus giving the variable no access modifier(so it can be accessed within that package) and accessing it by
ClassName.variable
What is the proper way, and does one or the other leave the variable in memory longer?
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jennifer,
There are no differences in memory. However, using get/set methods to give access to instance variables is the proper way to do things. The official term is "encapsulation" and it's your first step toward creating a good design for your components. The idea is that when you reveal your variables directly, other components will use them. And then, what happens when you need to change those variables somehow? The answer: bad things happen. When all clients use methods to access your object, you're free to change the "insides" however you feel fit as long as it doesn't break the interface you've provided for clients of your component.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To further clarify Nathaniel's point, say you have a variable that is only allowed certain values, such as hours on a clock.
If you let people directly access clock.hours, you have NO control over what they set it to. 18, 397, -46... you can't stop it.
however, if you use a setter method, if they call clock.setHours(96), you can ignore the value, do a mod 12 on it, throw an exception... anything you want. The point is, YOU have control over it.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the input guys! It is much appreciated.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic