• 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

private final static??

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Just a basic question on access modifiers. If a class has a string object that will be final, for example a particular error message and that String object is also private and can only be accessed using for example a getErrorMessage method, is there any logic in that string object also being static? Because the string is private it can only ever be accessed by an instance of the object in which it lives so is making it static redundant?
Is there ever a situation where a class attribute should be both private and static?
Cheers,
John
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi John,
There might be a situation where u want all your instance method to share a property(static), bcos this property is used by instance method so that we can keep it private.
Let me know your comments.
Cheeres
Chhhota Don
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static keyword indicates that the attribute is a class attribute, not an instance attribute, so if you don't need each instance to have it's own copy of that attribute and don't want any other classes to have direct acces to that attribute, you can use the static and final modifiers.
 
Greg Reinl
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you can use the static and final modifiers.


Sorry, I meant to say you can use the static and private modifiers.
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Greg Reinl:

Sorry, I meant to say you can use the static and private modifiers.



Yes , thank you. Not having a String object containing the error message in each instance of of my class is a good reason to make it static
 
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic