3

I just want to know, since I have a lot of private final static variables in my 3 classes, if It is a good idea to put them in just one classe like this :

public class Definicoes 
{
    /*
    * Definições da Janela de Jogo
    */

    public final static String nomeJanela;
    public final static short janLargura;
    public final static short janAltura;
    public final static byte tabLinhas;
    public final static byte tabColunas;
    static
    {
    nomeJanela = "JFindPairs - 0.3";
    janLargura = 480;
    janAltura = 480;
    tabLinhas = 4;
    tabColunas = 4;
    }

    /*
    * Definições do Tabuleiro e respectivas Cartas
    */

    public final static byte numeroCartoes;
    static
    {
        numeroCartoes = 16;
    }
}

Thanks in Advance for any help. Luis Da Costa

aliasbody
  • 816
  • 3
  • 11
  • 25
  • 5
    Your subject line says "private final static", but the example code shows "public final static". – Paul Tomblin Apr 29 '12 at 15:56
  • In the original class I used them as private, but here to access them in the other classes I need them as Public, this is why I ask. If it is a good idea to separate them from the original classes and make them public – aliasbody Apr 29 '12 at 15:58
  • 1
    Just about terminology: The keyword 'final' violates most common definitions of the word 'variable'. And why do you introduce a static block, and don't just write the assignment to the declaration: `public final static String nomeJanela = "JFindPairs - 0.3";? Are you get paid per line? – user unknown Apr 29 '12 at 15:59
  • Those look to be settings for a GUI game. Perhaps this should be stored in a settings file outside of the code, perhaps an XML file, since this seems like set up data that might change. – Hovercraft Full Of Eels Apr 29 '12 at 15:59
  • possible duplicate of [What is the best way to implement constants in Java?](http://stackoverflow.com/questions/66066/what-is-the-best-way-to-implement-constants-in-java) – Don Roby Apr 29 '12 at 15:59
  • By the way according to the Java Code Conventions `static final` fields should be named with all uppercase letters, numbers, and underscores, e.g. NOME_JANELA. – kapandron Apr 29 '12 at 16:07

4 Answers4

4

I would put them in one place if they all have to be the same. Otherwise, I would keep the constants where they are used.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

that's how I do it, It makes sense to have them all in 1 place in case you need to come back and change them at some point in time, don't want to have to search loads of classes for 1 variable :)

Skyste
  • 66
  • 4
1

When they start as private i.e. they are used only in a single class, they should stay there.

If they need to get accessed by different classes there is probably some concept hiding. The concept might manifest itself as a class with just public static final variables, but more likely it will split in multiple classes (at least that is my experience). Unfortunatly there is now way to tell with out knowing more about your program.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
1

It's completely valid to do so however it would break the concept of OOP design to decouple variables / constants that belong to a specific class.

Will Kru
  • 5,164
  • 3
  • 28
  • 41