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

13,340

Solution 1

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

Solution 2

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.

Solution 3

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

There are Code Conventions for the Java Programming Language and it says:

This Code Conventions for the Java Programming Language document contains the standard conventions that we at Sun follow and recommend that others follow. It covers filenames, file organization, indentation, comments, declarations, statements, white space, naming conventions, programming practices and includes a code example.

  • 80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

It states Naming Conventions, which says on class names:

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).

i.e. class Raster; class ImageSprite;

Is it not possible to run a program containing lower-cased class names? If it is possible, what's the difference?

It is possible, appropriate and there is no difference for compiler.

It is disrespectful for code and other programmers.

It is uncomfortable for you not to follow Java coding conventions.

Solution 4

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

It is a convention that is universally observed by experienced Java programmers.

Is it not possible to run a program containing lower-cased class names?

Yes it is possible. It is also possible to go to work in the same set of clothes for a month. But you won't make any friends by doing either.

If it is possible, what's the difference?

The difference is that your code is much less readable to other people if you ignore the conventions. Now, if you are writing code that will only ever be read by you, then code style is purely your problem. However, in the real world, most of us regularly have to read code written by other people. And if your code is painful to read because you've chosen to ignore the conventions, don't expect people to praise you for it.

Frankly, it is anti-social to write unreadable code in a context where there is a reasonable expectation that other people will need to read it.


I should add that it is not just anti-social. It turns out that you can burn yourself by ignoring the conventions. The style conventions are designed to work in conjunction with Java's identifier disambiguation rules; see JLS 6.5. If you ignore the style rules, you are liable to find that an ambiguous identifier (e.g. one that could be either a class name or a variable name) gets disambiguated in an unexpected way. The net result will be unexpected compilation errors that are (typically) hard to understand.

Solution 5

Java naming convention for class

  Classes: Names should be in CamelCase. 

Try to use nouns because a class is normally representing something in the real world:

class Customer
 class Account 
Share:
13,340
SKADIN
Author by

SKADIN

i'm here to learn.

Updated on June 06, 2022

Comments

  • SKADIN
    SKADIN almost 2 years

    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?