Java Naming Conventions

I wanted to write this short post to help certain people who are having a hard time memorizing the Java API classes and Method names. As you know Java is a case sensitive language and to build Java programs you need to use a lot of Built-in API classes and methods. And beginners find it really hard to memorize the method names and Class names exactly without changing the case.

But actually you need not remember the case. It would be overkill to memorize it. But if you follow the Java Naming conventions, then you need not memorize the case of the methods and classes that you will be using. 99% of the classes in the JAVA API follow this naming convention. Only 1% of the names break this rule and that is also due to programmers forgetting to name it properly (its true!). So here goes…

1. Classes:

Class names always begin with a capital letter (eg. java.util.Scanner). And if there are mutiple words in the class name then each word must also begin with a capital letter (eg. java.util.GregorianCalendar). Also package names always start with lowercase characters (util, lang, io, etc). And if there are multiple words in the package name, then you need to use uppercase for all words except for the starting word. This naming method is popularly called as UpperCamelCase which is a type of CamelCase! Interfaces also use same convention.

class MyClass {
}

2. Objects/Variables:

Java Naming convention specifies that instances and other variables must start with lowercase and if there are multiple words in the name, then you need to use Uppercase for starting letters for the words except for the starting word. This is called as lowerCamelCase.

String myName;
MyClass myObject;
Scanner scannerObject = new Scanner(System.in);

3. Methods:

Methods in Java also follow the same lowerCamelCase convention like Objects and variables.

void myMethod() {
}
String myName = scannerObject.nextLine();

4. Constant Variables:

In Java constant variables are declared using “static final” modifiers. And such variables must contain only UpperCase charachters and multiple words must be seperated using ‘_’.

static final char END_OF_FILE = 'e';
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Well thats it. Again, all these conventions were created just to improve readability of code. So its your choice to use them or leave them. But if you do use them, your code will look professional. Java Compiler does expect you to use these conventions. But there are some languages where, the way you name your variables, indicates to the compiler what type of variable it is. For instance, in Ruby, for declaring a constant variable you have to use only Uppercase for entire name of the variable. Ruby compiler identifies constant variables in that way only! Thank God, Java is flexible!

Additional Resources:

Reference: Java Naming Conventions from our JCG partner Steve Robinson at Footy ‘n’ Tech blog.

Related Articles:
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

3 Responses to "Java Naming Conventions"

  1. kishor says:

    i have read your tutorial it is nice one…..now i got the worth of naming convention in java..thanx

Leave a Reply


− three = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books