aspose file tools*
The moose likes Beginning Java and the fly likes Static Variable Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Variable Question" Watch "Static Variable Question" New topic
Author

Static Variable Question

Michael Salvini
Greenhorn

Joined: May 25, 2010
Posts: 20
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


Learning Java & Studying for the SCJA
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: May 25, 2010
Posts: 20
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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: May 25, 2010
Posts: 20
Sounds good to me, and again I appreciate the time and help.

Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Variable Question