Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should the first letter of a Java class be upper-cased? [closed]

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?

like image 881
SKADIN Avatar asked Aug 14 '12 06:08

SKADIN


People also ask

Do Java class have to be capitalized?

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.

Should class names be capitalized C?

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.

What does all caps mean in Java?

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.

Which of these naming convention starts with an uppercase?

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.


2 Answers

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

like image 189
MadProgrammer Avatar answered Oct 06 '22 03:10

MadProgrammer


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:

  1. foo is an instance of some class
  2. FooFactory is a class
  3. getFooInstance() is a static method of the class FooFactory
  4. doSomethingFoosDo() is an instance method of foo

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.

like image 21
jahroy Avatar answered Oct 06 '22 04:10

jahroy