Variables / Fields in Java

Declaring Variables

Before using any variable, give it a name and data type. This is known as declaring a variable. The syntax for declaring a variable is


Java objects stores states in fields. Java programming language defines the following kind of fields / variables.
Local Variables

Local variables are used to maintain the state of a method temporarily. The syntax to declare local variables is similar to data fields.

There is no special keyword in java to designate the local variable, the location of variables determine’s the scope of the variable is in between the opening and closing braces of a method. These variables are available with in the method they declared and they are not accessible from outside of that method.


The variables defined in method header are known as parameters. When a method is invoked , you pass a value to the parameter. This value is referred as actual parameter or argument.  Consider the signature of main method

The args variable is the parameter in this method.

Instance Variables

In java, objects maintain state in non-static fields. Non-static field means, a field declared without static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class. They don’t share the values with other instance variables of a class.  The id of a student is different then id of another student.

Class Variables

In java class variables are declared with static modifier. This indicate the compiler that there is only one exact copy of this variable exists, regardless of how many times the class is instantiated.  Such a field can be added in a Student class to count the number of student objects created

numberOfStudents field is shared among all the instances of a class, having same value for everyone. Additionally you can add final keyword to make it constant the numberOfStudents will never change.


[1] Sharon Zakhour, Scott Hommel, Jacob Royal, Isaac Rabinovitch, Tom Risser, Mark Hoeber, The Java™ Tutorial Fourth Edition: A Short Course on the Basics, Addison Wesley Professional

[2]  Oracle Website,,  Accessed on 05/04/2014

[3]  Ralph Morelli, R. W. (n.d.). Java, Object-Oriented Problem Solving – ISBN 0131474340. Prentice Hall.

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.