Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

private final static??

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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 am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic