Are class names allowed to be lower case [on hold]

2019-02-17 05:41发布

问题:

I ran my program successfully when declaring a class name starting with a lower case letter. I don't understood why it is asked to start with first capital letter.

回答1:

You can declare it with lower case, but the convention is to start with a capital letter. The conventions were created to make it easier on others to read and understand your code.

Unlike this definition :

class someClass 
{
    otherClass b = null;
}

Sticking with the conventions even helps Stack Overflow color your code in a way that makes it more readable :

class SomeClass
{
    OtherClass b = null;
}


回答2:

It's not a matter of can but rather a matter of should. Java naming conventions dictate that class names should begin with an upper case letter. By following conventions, others (including us and your instructors, bosses and co-workers) can better understand and evaluate your code. If you need our help in the future with your code, this can make a big difference. There can be some local variability in some specific rules, so you will want to learn and follow all the specific rules of your office / school.

For more on this, please see Java Naming Conventions.



回答3:

Also, some code editors/IDEs will hyphenate or space out related generated code file names based on capitalization in your class file.

For instance, Android Studio(https://developer.android.com/sdk/installing/studio.html) will read a Java Activity's class name, and insert a hyphen or underscore when you transition from a capital to a lower case letter for the file name of the layout.

An example: When creating a new activity(which is just a new class) called "MyActivity.java", Android Studio will also create a new layout file called "activity_my.xml" and link to it in the java file.

By sticking to the convention of capitalizing your class names, not only is your source code easier for others to follow and learn, but it will be much easier for you to navigate and keep track of files in your project. Naming conventions are everything.



回答4:

Its doesn't matter whether you choose uppercase or lower case but just for code to be readable without ambiguity.



回答5:

Nothing happens if class names are lower case or upper, as long as the the code runs, then it shouldn't be a problem.