public static String FrameTitle = "Заголовок окна", FrameLabel = "Лейбл окна", btn_1 = "Кнопка 1", btn_2 = "Кнопка 2", btn_3 = "Кнопка 3"; 

The application works, but is it customary to write this in Java? It’s just inconvenient for each variable to write "public static String ...". But also record

 public static String FrameTitle = "Заголовок окна", FrameLabel = "Лейбл окна", btn_1 = "Кнопка 1", btn_2 = "Кнопка 2", btn_3 = "Кнопка 3"; 

looks unreadable. Are there any other, maybe more correct options?

  • For example, the Google Java Style Guide google.imtqy.com/styleguide/javaguide.html prohibits this. See clause 4.8.2.1. - Kirill Malyshev
  • This is more a matter of personal preference. I, for example, unite in a "group" only interconnected constants - as a rule, such mini-enums. What is really not accepted is to write the field names with a capital letter. And you deliberately wrote static without final - but are they not constants after all? And finally - should these values ​​really be public ? - Regent
  • Thanks for the comments. Without public I won't get to them from other classes? I have these lines in a separate class, and indeed I plan to create all the lines in it - if you suddenly have to change them, you will not need to look for them somewhere in the code. - Daniel
  • According to Google Java Style Guide - to pull out a line, every time you have to contact the entire array ... interesting .. I'll take note. - Daniel
  • @Daniel, if you answered in the comments - you can simply ask the author of the comment to copy the text of the answer. If you do not copy it in a day or two, feel free to post as an answer yourself, with the note “the answer is given in the comment by such and such”. Free Reputation. - PashaPash

1 answer 1

If the records are compiled, then they are correct :) You can do as you please in your code if you write for yourself and for yourself. A matter of taste. If you are writing open source, or in a team, then to further support your code by other programmers, it is better to adhere to generally accepted standards, which can be found upon request of the Java Code Conventions, for example, here: https://www.oracle.com/technetwork/java/ codeconventions-150003.pdf Mark each variable with a new string.

If you have a separate class for constants, then make an interface and declare your constants in it. In the interface, all fields have a public static final final modifier, and this extra text for each line will not have to be written.