Are class names allowed to be lower case [closed]

断了今生、忘了曾经 提交于 2019-12-19 06:46:32

问题


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:


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




回答5:


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



来源:https://stackoverflow.com/questions/25119757/are-class-names-allowed-to-be-lower-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!