89

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?

Melebius
  • 6,183
  • 4
  • 39
  • 52
csss
  • 1,937
  • 2
  • 14
  • 24
  • 4
    `static final String WELCOME_MESSAGE = "Hello";`? – Oliver Charlesworth Mar 09 '12 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. – csss Mar 09 '12 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. – yshavit Mar 09 '12 at 19:01

7 Answers7

158

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.

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
  • 2
    I've always wondered, isn't it unnecessary to set the constant as 'static' if you define the constant as 'private'? – ChallengeAccepted Jun 25 '16 at 08:38
  • 9
    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 http://stackoverflow.com/q/1415955/247763 – derekerdmann Jun 27 '16 at 16:32
  • 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. – dko Dec 06 '21 at 17:34
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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
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.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • 1
    You wrong. It doesn't help to save memory because of string pool, used by JVM. – Mirimas May 05 '17 at 09:06
  • @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. – Pritam Banerjee May 10 '17 at 21:25
  • 3
    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: http://stackoverflow.com/questions/3297867/difference-between-string-object-and-string-literal – Mirimas May 11 '17 at 07:32
6
public static final String YOUR_STRING_CONSTANT = "";
RePRO
  • 265
  • 6
  • 18
5

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.

V_Singh
  • 729
  • 11
  • 22
5

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

2

simply use

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

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

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66