Java Local Instance Variables

Java Local Instance Variables

Java Local Instance Variables

Summary: Explained in Java syntax in a simple way of using "Java Local Instance Variables" for a beginner.

Variables – Properties

A program stores values in variables. As the name indicates, a variable value can be varied by the programmer at any time. An object uses variables to set its properties. For example, a motor car uses the properties like speed and petrol etc. by storing them in variables. The declaration of a variable requires a data type. A data type says what type of value a programmer can store in the variable.

Using Local Variables

The variables declared within a method body are known as "local variables". The method parameters are also treated as local variables only. As their accessibility (or scope) is limited to the method only, the local variables can be used within the method body only. That is, other methods cannot use them.

Java Local Instance Variables

Observe, the variables mangoes and bananas are declared in the body of the main() method. They can used within the main() only.


To print the values of variables, it is not required to use the place holders like %d or %f etc. Just writing directly mangoes, prints the value of the mangoes variable as in the above statement.

System.out.println("No of mangoes: " + mangoes);

In the above statement, + operator works for concatenation of a string and a variable value.

System.out.println(mangoes + " and " + bananas + " sum is " + (mangoes+bananas));

Java does not support operator overloading but designers overloaded the + operator. In Java coding, + operator can be used both for addition and concatenation. In the above statement, + is used for the concatenation of " and " and also used for addition as in "mangoes+bananas". To get the correct values, if there exists a string in println statement, the variables that are to be added are to be placed in parenthesis. Observe, mangoes+bananas are placed within parenthesis.

Basic Constructs – Objects

Java being an object-oriented language, objects play an important role. Every real time entity can be treated as an object. For example, a motor car can be treated as an object. Variables are used to set the properties for an object. To manipulate the variables, methods are used. Methods are called with objects. So variables, methods and objects constitute a program. An object is a handle with which all the code of a class can be accessed or manipulated.

A number of objects can be created for a single class. Each object can be given properties through variables. OOP permits to have a separate set of values for each object. Methods increase the reusability of code. Methods of Java are called as procedures or functions in other languages.

Let us define what an object is. A C++ programmer says, an instance of a class is known as object. Of course, he is right but the definition is somewhat confusing. Let us put it more explanatory. Suppose, when we cut the tree, we get the wood. Wood, as it is, is useless until and unless it is converted into objects like door, table etc. Without the articles like door or table, wood is waste. These articles are termed as objects in an OOP language. A class comprises of methods, constructors and variables etc. To use the class we require an object. So, the object represents the whole class. We can treat object as a handle with which the whole class can be handled. Without object, the class is useless as it cannot be used. In our first simple program, the name of the class is “Wishes” and includes only one method, main() method. We did not create any objects and we do it in the next program.

Using Instance Variables from static main()

Java is a strongly-typed language where variables should be declared and given values before they are used. Java coding uses both local variables and global variables. The global variables are known as "instance variables" in Java. Instance variables are to be called with instances (objects) from static methods like main(). It is to be noted that local variables do not require an object. The scope of instance variables is for whole class and local variables are within the method in which they are declared. In the following program local and instance variables are used.

Java Local Instance Variables

Output screen of

In the above Employee class, one local variable "company" and two instance variables "salary" and "department" are declared. They are used from the static main() method. As you can observe from the above program, to call the local variable "company", object "emp1" is not used; but to call the instance variables emp1 is used. If not, the compiler raises errors (as in the last statement).

Employee emp1 = new Employee();

First time we are creating an object of a class. Note the syntax. The class name comes two times. Employee is the class and Employee() is known as a constructor. “new” keyword is used to create an object.

emp1.salary = 8000;
emp1.department = “Finance”;

In the above two statements, to give values for the instance variables salary and department, object emp1 is used.


To print the local variable company, object is not used. To print the instance variables, object emp1 is used.

12 thoughts on “Java Local Instance Variables

  1. Barathi

    Sir in above explanation of Objects i have a clarififcation.
    so you are saying wood is a class and door, table as its object.
    Am i right?

Leave a Reply

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