Why do constants in the File class not follow the constant convention?

Asked

Viewed 113 times

6

The convention for Java constants says that a constant should be declared with uppercase letters only and words should be separated by underscore (_).

The declaration of a constant is made by the sequence of keywords static and final. Ex: public static final String NOME_COMPLETO = "José da Silva";


The class File has its stated constants in camelCase. That is, it does not follow the convention. Several other classes of Oracle itself follow the convention.

Why is this different? Is there any other convention that "overrides" this one? Or has the convention been ignored?

inserir a descrição da imagem aqui

  • 1

    Now I saw, maybe it is because these variables do not have a value already defined before the class is started. Look what the definition of the 3 variables says: The system-dependent[...], i.e., the value is only defined when using the class, for the compiler to identify which is its OS.

  • @diegofm Good! Then there is another convention for this case?

  • 1

    Most likely. The 3 variables are not part of the constant fields with default values.

  • @diegofm Legal. This appears to be the answer. If you are sure, could you please reply?

  • 1

    I have read somewhere that there is a lot of inconsistency even by accident. I do not know if it is so much the case.

1 answer

7


In fact these variables are not constants, they are variables with a final reference, probably obtained when this class is used, since the separator is defined according to the operating system running.

This becomes more obvious by looking at class source code, how these variables are declared:

public static final char pathSeparatorChar = fs.getPathSeparator();

public static final String pathSeparator = "" + pathSeparatorChar;

public static final String separator = "" + separatorChar;

public static final char separatorChar = fs.getSeparator();

Realize that separatorChar and pathSeparatorChar has its reference values retrieved from FileSystem, which is the class that stores information about the local file system of the system where the compiler is running.

So these variables ended up following the standard of variable convention camelCase, because they are not constants.

As can be seen this documentation link, there is a list containing all the constants of the java API, and it can be observed that they all follow the nomenclature pattern for constants, and class variables File are not on this list.

References:

  • Do you see any reason why they’re not methods rather than variables?

  • @Igorventurelli believe it is to facilitate the use, although this may have been a matter of taste of the java developers. Not to mention seeing something like path = "path" + File.separator + "file;" I think it’s much nicer to read than path = "path" + File.getSeparator() + "file;"

  • Yes. But the way it is implemented seems to be much more familiar to the properties of C# than the same Java development. No matter how "ugly", the getSeparator() it would be something very close to the day to day of the Java dev, agree?

  • 1

    @Igorventurelli Apparently this method already exists as an abstract in the class FileSystem, may have avoided creating in the File class to avoid redundancies, since the two are related to each other.

  • 1

    @Igorventurelli reason to be field final instead of method: they are values customarily consumed, so direct access to the field is more performatic. How much is used, this little difference can be crucial

  • 1

    @Igorventurelli , may not have been the original intention, but performance is a good excuse ;-)

  • @Jeffersonquesado good one! This makes sense, but if it is a static field, the values will be occupied in memory when the class is called and will stay there until the application closes... It can even be more performative, but has greater memory consumption right :P Remember that we are talking about 4 fields

  • I tried to find it from Java, but it serves as a metaphor: http://www.alltechflix.com/wp-content/uploads/2015/05/why-chrome-eats-too-much-ram.jpg

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.