Java Naming Conventions: Variables & Constants

Instructor: Sudha Aravindan

Sudha has a Doctor of Education degree in math education and is currently working as a Information Technology Specialist.

Sue has friends named Jane and Jayne. Are they the same friends? Probably not. Why? Because their names are spelt differently! Similarly the Java programming language has naming conventions for variables and constants. In this lesson we will learn about these conventions.

Why Are Naming Conventions Important?

In real life, we know that even if names sound the same (like Jane and Jayne) they are two different people. The same logic applies for computer programming languages also. If we need to differentiate variables and constants from one another, each variable and constant has to have a unique name.

In addition, the Java programming language follows naming conventions to make the program easier to read, to eliminate compilation errors, and to identify the name and purpose of the variable. So for example, if we see the variable name studentID, we would know that is the ID number of a student. And if we see the a name like FIRST_NAME we can tell that this is the first name of a person.

Java Naming Conventions for Variables

The Java programming language has naming conventions or rules that you need to follow when naming variables in Java. If you break the rules, the program will not compile and will generate an error. Let us now consider some of the more common naming conventions for variables in Java.

Variable Names Are Case Sensitive

So for example, int initialValue is different from int InitialValue or int Initialvalue

Consider the following example:

class JavaVariable {
  static void variableValues() {
   int initialValue = 0;
   System.out.println("Initial value of varialbe is : " + InitialValue);
  }
  public static void main(String[] args) {
   JavaVariable jv = new JavaVariable();
   jv.variableValues();
  }
}

Here the name of the variable that is initialized is initialValue where only the V is capitalized. However the name of the variable that is printed is InitialValue with both I and V capitalized. When compiled this program generates an error as follows:


2aa


This is because in the println statement Java encounters the variable InitialValue that has not been initialized in the program. Only the variable initialValue has been initialized. Even though in English language terms initialValue and InitialValue may sound the same, for Java they are both two different variable names.

Variable Names Cannot be Java Keywords

In the Java programming language, keywords are words that have a pre-defined meaning for the language. So these keywords cannot be used to name variables. Some examples of Java keywords are: case, break, char, int, if, else, static, public etc. Consider the following Java program that uses a Java keyword static to name a variable:

class JavaVariable {
  static void variableValues() {
   int static = 0;
   System.out.println("Initial value of varialbe is : " + static);
  }
  public static void main(String[] args) {
   JavaVariable jv = new JavaVariable();
   jv.variableValues();
  }
}

An attempt to compile and run this program generates a number of errors, one of which says error: not a statement. This is because static is a Java keyword and cannot be used to name a variable.


4aa


Now suppose we compile and run the exact same program, but this time change the variable name from static to static1:

class JavaVariable {
  static void variableValues() {
   int static1 = 0;
   System.out.println("Initial value of varialbe is : " + static1);
  }
  public static void main(String[] args) {
   JavaVariable jv = new JavaVariable();
   jv.variableValues();
  }
}

The program now compiles and runs successfully generating the desired output:


6aa


Main is Not a Keyword in Java

Interestingly main or Main is not considered a keyword in Java, so can be used to name variables. Consider the following Java program:

class JavaVariable {
  static void variableValues() {
   int main = 5;
   System.out.println("Initial value of varialbe is : " + main);
  }
  public static void main(String[] args) {
   JavaVariable jv = new JavaVariable();
   jv.variableValues();
  }
}

This program compiles and runs successfully and prints out 5 on the screen as follows:


8aa


Java Variable Names Cannot Have Spaces

Consider the following java program. Here the variable is named initial Value with a space.

class JavaVariable {
  static void variableValues() {
   int initial Value = 5;
   System.out.println("Initial value of varialbe is : " + initial Value);
  }
  public static void main(String[] args) {
   JavaVariable jv = new JavaVariable();
   jv.variableValues();
  }
}

This program generates compile errors because it does not recognize initial Value (two words, with a space in between) as a valid Java variable name.


10aa


Now suppose you run the same program where instead of a space initial Value, you use an underscore, initial_Value:

class JavaVariable {
  static void variableValues() {
   int initial_Value = 0;
   System.out.println("Initial value of varialbe is : " + initial_Value);
  }
  public static void main(String[] args) {
   JavaVariable jv = new JavaVariable();
   jv.variableValues();
  }
}

The program now compiles and runs without any errors and generates an output:


12aa


Java Naming Conventions for Constants

Use Upper Case with Underscore

One important naming convention for constants in Java is to use all upper case letters with the underscore character separating the words. So for example use FIRST_NAME instead of firstName or FirstName.

As we have already seen, constants should have the word final before them. So, you would declare in the following manner:

final FIRST_NAME = "Sandy";

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support