I am trying to put all the String in a separate public class, the compiler is against, how to persuade it? My texts for the user are quite long, if you remove them from the main class, the code will not be overloaded with unnecessary text and will 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.

  • one
    make everything public static final String . - pavel
  • did not accept. In class, main cannot define the variable "hello cannot be resolved to a variable" - CoffeJava
  • In the class stringStore, too, not everything is ok. "Syntax error on token 'final', interface expected after this token" - CoffeJava
  • one
    Because your hello is defined inside the stringstore class, you need to call it as (new stringstore ()). hello - Mikhail Chibel

4 answers 4

A class with variables is declared like this:

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

Further, use:

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

You can also use import static :

 import static StringsStore.hello; import static StringsStore.w_up; import static StringsStore.bye; //Или импортировать все поля класса //import static StringsStore.*; public static void main(String[] args) { System.out.println(hello); System.out.println(w_up); System.out.println(bye); } 
  • one
    very bold about 1.7+ - Stranger in the Q
  • Thank you, this is a very detailed and understandable answer. - CoffeJava
  • Well, static imports appeared at 1.5 docs.oracle.com/javase/1.5.0/docs/guide/language/… - Stranger in the Q
  • Thanks, corrected. - cache
  • If StringsStore is made an interface, then you can simply implement it where necessary and all constants will be available. This is an option. - Russtam

Use standard java properties. Put all your lines with any keys into the messages.properties file and then use ResourceBundle to get the string value by key. For example, having:

messages.properties

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

To get a 'hello' string

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

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

  • Do you recommend for AndroidStudio? Not for Eclipse, right? - CoffeJava
  • one
    For eclipse will work too - Mikhail Chibel

Make the lines static and they will be accessible from anywhere.

 public static final String 

    To display the message, it is better to implement a separate class or method.

     public class Main { public static void main(String[] args){ PrintText print = new PrintText; print.print(); } } class PrintText { private String hello = "Здравствуйте дорогие друзья"; private String w_up = "Очень рад, бла-бла-бла. Как поживаете?"; private String bye = "Ну всё пока, потом ещё куча текста..."; public void print() { System.out.println(hello); System.out.println(w_up); System.out.println(bye); } } 
    • This is the case if I have to print all the lines at once, so to speak in a package. And if only where I need and only what I need? - CoffeJava
    • one
      You create a variable, for example, in the method Final String str = "somestr"; And call the System.out.println (str) method; Or, in the same class, create a string as private final String (private static final String) if the method in which the string is used is not static. Or private static final String, if the method is static; - jisecayeyo pm