95

I have a list of constant strings that I need to display at different times during my Java program.

In C I could define the strings like this at the top of my code:

#define WELCOME_MESSAGE "Hello, welcome to the server"
#define WAIT_MESSAGE "Please wait 5 seconds"
#define EXIT_MESSAGE "Bye!"

I am wondering what is the standard way of doing this kind of thing in Java?

3
  • 4
    static final String WELCOME_MESSAGE = "Hello";? Commented Mar 9, 2012 at 18:21
  • 4
    Yes but I read some websites where they were saying 'final' is not a constant in java, so i wasnt sure. Commented Mar 9, 2012 at 18:25
  • 16
    @csss final in Java means the reference can't be changed -- but the object it points to still might. Luckily for us, String in Java is an immutable class, so a final String is const in both regards. Commented Mar 9, 2012 at 19:01

7 Answers 7

166

Typically you'd define this toward the top of a class:

public static final String WELCOME_MESSAGE = "Hello, welcome to the server";

Of course, use the appropriate member visibility (public/private/protected) based on where you use this constant.

Sign up to request clarification or add additional context in comments.

3 Comments

I've always wondered, isn't it unnecessary to set the constant as 'static' if you define the constant as 'private'?
No, it should still be static. Making it private and non-static would still create a new copy of the string each time you instantiate the type. See stackoverflow.com/q/1415955/247763
second @derekerdmann .. Not only that but, I just spent hours trying to debug my program failing, not realizing that the culprit all along was that i used only final String and the value I set was not actually able to be accessed in other calling classes.
14

It would look like this:

public static final String WELCOME_MESSAGE = "Hello, welcome to the server";

If the constants are for use just in a single class, you'd want to make them private instead of public.

Comments

6
public static final String YOUR_STRING_CONSTANT = "";

Comments

6

You can use

 public static final String HELLO = "hello";

if you have many string constants, you can use external property file / simple "constant holder" class

Comments

6

Or another typical standard in the industry is to have a Constants.java named class file containing all the constants to be used all over the project.

Comments

6

We usually declare the constant as static. The reason for that is because Java creates copies of non static variables every time you instantiate an object of the class.

So if we make the constants static it would not do so and would save memory.

With final we can make the variable constant.

Hence the best practice to define a constant variable is the following:

private static final String YOUR_CONSTANT = "Some Value"; 

The access modifier can be private/public depending on the business logic.

3 Comments

You wrong. It doesn't help to save memory because of string pool, used by JVM.
@Mirimas Nope. You are wrong here dude. Static is initialized only once, but a non static variable is initialized each and every time an object of that is initialized.
Here used string pool, because you don't use "new" keyword to create string. So, if even you don't use static for strings, it will used already created reference from the string pool. See more: stackoverflow.com/questions/3297867/…
2

simply use

final String WELCOME_MESSAGE = "Hello, welcome to the server";

the main part of this instruction is the 'final' keyword.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.