JAVA thong into a separate class

Question:

I'm trying to put all String into a separate public class, the compiler is against it, how can I persuade him? My texts for the user are quite long, if they are removed from the main class, then the code will not be overloaded with unnecessary text and become more readable?

public static void main(String[] args){
    System.out.println(hello);
    System.out.println(w_up);
    System.out.println(bye);
    }

public class stingsStore{
String hello = "Здравствуйте дорогие друзья";
String w_up = "Очень рад, бла-бла-бла. Как поживаете?";
String bye = "Ну всё пока, потом ещё куча текста...";
}

Thank you.

Answer:

Use standard Java Properties. Put all your strings with some kind of keys in the messages.properties file and then use the ResourceBundle to get the value of the string given the key. For example, having:

messages.properties

hello = Здравствуйте дорогие друзья
w_up = Очень рад, бла-бла-бла. Как поживаете?
bye = Ну всё пока, потом ещё куча текста...

To get the 'hello' string

String basename = "messages";
ResourceBundle.getBundle(basename).getString("hello");

Of course, this solution looks more complicated than string constants, but it allows you to separate the content of the texts from the code, and you can then translate the strings into another language when you enter the international market. Yes, still, properties support multiline data.

Scroll to Top