Why should the first letter of a Java class be upper-cased? Is it not possible to run a program containing lower-cased class names? If it is possible, what's the difference?
Interface names should be capitalized like class names. Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.
Do capitalize the first letter of class names. There are any number of rules for names that contain multiple words, such as camelCase, UpperCamelCase, using_underscores, etc. Again, pick one style and use it consistently within your code.
Basically, back when people were in love with C, and C++ & Java were either new or not yet created, people used all caps for preprocessor constant names, to indicate that they weren't actually variables.
The general convention for naming classes in Java is just that the first letter should always be capitalized and the whole name should be in camel case, meaning that the first letter of each word is capitalized.
It's a coding convention, adopted by most Java programs. It makes reading code easier as you become use to a given standard.
No, you don't have to follow it, but you won't make any friends by not doing so ;)
Why / how was this convention established in the first place?
The convention was established by Sun/The creators of the Java language.
It was likely establish to improve the promotion of the language across a wide and diverse audience, to lower the entry level requirements for new developers (much easier to learn if you don't need to keep switching between other people's "ideas" of what good code looks like) and generally to try and keep the code clean within the community.
You could ask "why do companies have style guidelines for coders?" and the reasons would, generally, be the same
Using naming conventions makes it easier to quickly understand code when you read it.
Here are two simple lines of code:
foo = FooFactory.getFooInstance();
foo.doSomethingFoosDo();
If we know the author follows naming conventions, we quickly know several things:
Otherwise we waste time wondering things like "is FooFactory an object or a class?"
Without naming conventions, you'd actually have to search through the source code to answer such questions.
This seems overly uptight at first, but it makes an ENORMOUS difference as you begin to write more complex software and work with others.
You'll find that naming conventions even make your own code more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With