java variables

Java Variables – The Building block

Java is an object oriented, high level programming language, which was given to the world by Sun Microsystems, in 1995. It has unique distinct feature of Write Once, Run Any Where, giving ease of programming as programmer need not to worry about the platform on which he will run the program. But as any programming language it has its basic building block called as  Variables (Java Variables), which are required to be there to hold state of the program in the memory of the machine. Its very important to know about them as a Java Developer should keep these basics in his mind before writing any program to define the minimum required memory by the program.

Lets jump into the Java Variables’ world and try to understand the concept first. Read thoroughly and than pause for a moment to ponder upon your understanding of the concept (If you still feel something is not clear, comment below).

Types of Java Variables:

There are four types of Java Variables which are described as follows:

1.Class Variables: The Variables which are defined with a static modifier are called as class variables. These variables have only one copy per class and all objects of that class shares this copy. When state of such variables is changed by any object, it gets reflected to all the objects. They will be in memory till class is loaded.

2.Instance Variables: The variables which are defined inside class body, but outside method body without any static modifier are called as instance variable. They belong to the object of class and have a separate copy per object. They will be in memory till object is in memory.

3.Local Variables: The variables which are defined inside method body are called as local variables. They don’t have any modifier with them. Their scope is limited to method and as soon as method returns they are removed from the memory.

4.Parameters/Arguments: The variables which are defined in method signature are called as arguments or parameters. They to don’t have any modifier with them and their scope is within method and they are removed from the memory as method returns.

That was pretty theoretical and doesn’t make sense to many as they might be lost in terminologies, so here comes the example which will ensure that you get to understand these better.

public class varExample{
    static int var1;
    int var2;
    public void doSomething(int var3){
        int var4;

The variables given in above program describe above given Java Variables’ type. You can take pause and guess what they could be and then match them with following details to test your understanding.

var1 // class variable
var2 // instance variable
var3 // parameter/argument
var4 // local variable

I hope it makes some sense now.

Creating Java Variables, Assigning values and memory:

Java variables are declared with following syntax:

data_type variable_name;


int var1;
int var1,var2,var; // All 3 variables are of type int

But you can add access modifiers to them if they are class level or instance variables. Access modifiers are added before data type.

private int var1;

Now we will assign value to the variable

int var1;
var1 = 1000;

Assigning value can be done while declaring a variable too.

int var1 = 1000;
int var2 = 1001, var3 = 1003; // This is valid too and var3 is of type int

So when we are declaring and assigning a value what are we doing, we are taking a memory space in computer’s memory which is as per data type used while declaring variable, but to remember that we need friendly name which is the variable name. The variable name is called as Identifier (in technical terminologies) and values are called as Literals.

Valid Naming for Java Variables:

  1. The variable name can contain characters from A to Z and from a to z.
  2. The variable name can contain numbers from 0 to 9, but they are not allowed in beginning of the variable name.
  3. The variable name can contain $ and _.
int 0starCount; // its illegal name
int startCount; // its perfectly legal
int _starCount; // legal
int $startCount; //legal
int $star0192Count; //legal
int _$12412412; //legal

So now you know what a legal variable name looks like, so now just keep in mind it should make sense too, otherwise your code won’t be readable.

Happy Coding!!!

Related Posts

Leave a Reply

Your email address will not be published.