8

What is the naming convention of classes in java, for example should all classes be in upper case like MYCLASS.java ?

as some classes like com.sun.org.apache.bcel.internal.generic. ANEWARRAY. can be found in Sun's library as well

Note: I have read all naming convention from Oracle but I could not find anything which says we should name a class with All Uppercase.

ankit
  • 4,919
  • 7
  • 38
  • 63
  • 4
    Everybody just uses camel-case for class names, so I would probably just stick with that. – Supericy May 14 '13 at 07:43
  • In some programming languages Uppercase words are used for constants. It could be confusing. – RST May 14 '13 at 07:45
  • 1
    The "standard" convention: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367 – NilsH May 14 '13 at 07:45
  • 1
    The only case I can imagine (according to convention) that this is an abbreviation: ANiceEggWasAtRoughRoundArrayYeti – Michal Borek May 14 '13 at 07:46
  • 4
    *"Like we have some classes in com.sun.."* Remove them immediately. Do not put classes in the `com.sun` package (unless you are Sun, or bought the company). – Andrew Thompson May 14 '13 at 07:54
  • 1
    I think he is referring to [Class ANEWARRAY](http://commons.apache.org/proper/commons-bcel/apidocs/org/apache/bcel/generic/ANEWARRAY.html) although the `com.sun` should not be part of the package name. – maba May 14 '13 at 07:59
  • @AndrewThompson I have not put this class in com.sun.... its already a part of Sun's library – ankit May 14 '13 at 08:20
  • @supericy I know we use camel-case but I just wanted to know is there any naming convention which suggests us to use all Uppercase – ankit May 14 '13 at 08:22
  • @maba you are right I am referring to class ANEWARRAY and com.sun is not part of my package structure some one had edited my post and made it confusing – ankit May 14 '13 at 08:26
  • @ankit "I could not find anything which says we should name a class with All Uppercase." Why do you think there would be? Thats not the convention – Richard Tingle May 14 '13 at 08:32
  • It's pascal case. – Muhammad Babar Dec 01 '16 at 05:35

5 Answers5

16

Class Names should be in CamelCase. Try to use nouns because a class is normally representing something in the real world.

The Documentation states the following:

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

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • 13
    What you described is actually Pascal casing. Camel casing has the first letter lower case. – Mike Young Jan 27 '16 at 23:20
  • Any idea if there's a more modern document specifying these conventions? The document you've linked to is marked as unsupported and last edited in 1999. – dimo414 Apr 25 '17 at 23:30
4

Java has a very well described naming / coding convention.

You can look it up here http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html

Technically it doesn't matter how you name you classes as long as public classes are in a .java-source file with the same name as the class.

Ozzie
  • 11,613
  • 4
  • 21
  • 24
1

The quoted class com.sun.org.apache.bcel.internal.generic.ANEWARRAY looks to be from the deep innerworking of Java (internal.generic), i.e. not for developer use. As such its really outside of the naming convention. I can only speculate as to why its all in capitals, perhaps to emphasise this point that it shouldn't be used.

Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
  • sorry to say but, you had made my question confusing I have already read all the naming conventions in java and I could not find anything telling class name should be all Uppercase, thats why I asked this question. – ankit May 14 '13 at 08:28
  • @ankit Thats because classes *shouldn't* be all in uppercase – Richard Tingle May 14 '13 at 08:29
  • com.sun.org.apache.bcel.internal.generic. ANEWARRAY this is a class provided by sun and it has name in all uppercase. – ankit May 14 '13 at 08:33
  • @ankit Ahhh, the way you originally wrote your question it seemed (not just to me) like you had created that class yourself – Richard Tingle May 14 '13 at 08:34
  • I have corrected it now, and sorry for the misleading question but your edit added oil to fire ;). I think both of us have learnt something isnt it ? – ankit May 14 '13 at 08:38
  • @ankit Have updated answer, although its more of a "Don't worry about it" answer than anything else – Richard Tingle May 14 '13 at 08:45
0

Usually the best practise is to use Upper CamelCase.anyway there will be no compilation issues with other conventions.see more details about conventions

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
0

After browsing through many links I came to know there is no java convention which suggests us to name a class in all uppercases. However we do so there will not be any error.

One appropriate reason is mentioned by CloudyMarble that we can use this convention if the abbreviation is more famous like HTML.

Also I would like to add some info about ANEWARRAY, this class comes under Apache license Source and Create new array of references.

Community
  • 1
  • 1
ankit
  • 4,919
  • 7
  • 38
  • 63