Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Variable Question

 
Michael Salvini
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Morning All,

I am pretty new to Java and I have been spending lots of time creating small applications for practice and on one of them, I wanted to have the version appear in many different parts of the gui (login window, title bar, help window, etc). I create a class called StandardText with some static variables marked private. I have pasted some sample code below. I then created some getter methods to retrieve the static variables and then I was sitting here thinking that why do I need the getter methods, the variables are already static, if I mark them public, I will be able to access them direct. My question is two parts:

1) Which way is the better method of practice, private static variables with getter methods that are also static or public static variables with no getter method?

2) Is there are better practice of doing what I am doing?



Thanks,
Mike
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion- public static final is better. That way the reader will immediately understand that the variable is a constant. Why simply have another getter and increase the verbosity of the code.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you let the code access the variable directly then there is no way (without rewriting your code) to add behaviour to what you return. For instance if you later decide to make you program multi-lingual then you have to rewrite your code. If you accessed it with a getter then you only need to change your getter method.

In practise it is almost always better to use getter/setter methods because of these reasons.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Wouter. Its always best to encapsulate your variables.

I was thinking of situations where there are constants- something like PI in Math.

We would want to declare them as public static final and then for multilingual- We make use of the corresponding the Static variable.
 
Michael Salvini
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes sense, thanks for the help. I will leave it how I started to set it up.

I don't know what I would do if I didn't have a place like this to ask these questions.

Thanks again.

Mike
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Salvini wrote: I don't know what I would do if I didn't have a place like this to ask these questions.

Now that we have, lets make maximum use of this
 
Michael Salvini
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good to me, and again I appreciate the time and help.

Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic