Defining constant string in Java?
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?
Solution 1:
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.
Solution 2:
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
.
Solution 3:
public static final String YOUR_STRING_CONSTANT = "";