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.
public class MethodVariables { public static void main(String args[]) { int mangoes = 10, bananas = 20; System.out.println(mangoes); System.out.println(bananas); System.out.println(mangoes + bananas); System.out.println("No of mangoes: " + mangoes); System.out.println(mangoes + " and " + bananas + " sum is " + (mangoes+bananas)); } }
Observe, the variables mangoes and bananas are declared in the body of the main() method. They can used within the main() only.
System.out.println(mangoes);
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.
public class Employee { int salary; // instance variables String department; public static void main(String args[]) { String company = "Lorvent Solutions"; // local variable System.out.println(company); // calling local variable without object Employee emp1 = new Employee(); emp1.salary = 8000; emp1.department = "Finance"; System.out.println(emp1.salary); // calling instance variables with object System.out.println(emp1.department); System.out.println(emp1.department + " section exists in " + company); // System.out.println(salary); // compilation error } }
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.
System.out.println(company);
System.out.println(emp1.salary);
System.out.println(emp1.department);
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”
Thank u …it’s very easy to learn here.
what is a constructor
See this link:
sir,what is the difference between member and variable?
Methods and instance variables are known as members of class. Constructors are not members.
This is the best site for java beginners.
Can you please put servlets,jsp,struts and webservices also.
Thank you sir.
Just I stated servlets. It is a hobby for me to write. Actually, I am a very busy man in Hyderabad.
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?
Yes, you are very right in your thinking.
well appreciated site :)
real time examples are wonderful,, it is easy way to understanding java::
thank u sir.
This is the best site for Java I’ve ever seen…Thank u sirji…!