简体   繁体   中英

What is the naming rules for public static final class identifier in java?

Java Naming rules suggests which one to be used. This

public static final class DatabaseTable

or, this

public static final class DATABASE_TABLE

Which one is correct?

I think first one is appropriate. Any class name that may be final or whatever, it's name should be in proper case without any space . Proper case means first letter of each word should be in Upper Case and remember don't use any UnderScore in this case.

You should name in the second style if it is a final variable

There is no such thing as "incorrect" here, in the sense that the Java compiler or syntax of the language does not force you to use specific naming conventions.

If we look at the old Code Conventions for the Java Programming Language document, specifically Chapter 9 - Naming Conventions , we see that classes should have CamelCaseNames:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

It doesn't matter if the class is a nested class ( static ) or if the class is final .

ALL_UPPERCASE_NAMES are for constants; static final variables (not classes).

fields with public static final modifiers are used as "constants" in Java and should be written in capitals. So the correct (compilable) statement would be:

public static final Class DATABASE_TABLE = DatabaseTable.class;

In case you were looking for a classname, then the naming convention is as usual for classes: first letter is capital, the name in CamelCaseNotation.

public static final class DatabaseTable {
  // ...
}

我仍然会使用第一个,因为CAPS通常是指CONSTANT字段,而不是最终方法或最终类,即使它们是静态的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM