Variable Types in Java

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java


Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

Variable Types in Java
Previous Home Next

There are three types of variable in java

  1. Local variable.
  2. Instance variable.
  3. Static variable.

Local variable

  1. A variable that is declared inside the method is called local variable.
  2. A local variable in Java is a variable that's declared within the body of a method. Then you can use the variable only within that method.
  3. Local variables are declared in methods, constructors, or blocks.
  4. Local variable can't be declared as static. Two local variables can not have same name if there scopes are same.
  5. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
  6. Access modifiers cannot be used for local variables.
  7. Local variables are visible only within the declared block.
  8. There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.

Example :

class Local
	public static void main(String args[])
		int i=10;
		int j;
		System.out.print(j);//Variable j might not have been initialize 
		for(int i=1;i<=5;i++)
			System.out.print(i);//error of duplicate variable 

Instance Variable

  1. A variable that is declared inside the class but outside a method, constructor or any block is called instance variable. It is not declared as static.
  2. These variables are declared at the top level. They begins their life when first class loaded into memory and ends when class is unloaded.
  3. There will be as many copies of the instance member variables as there are number of objects(one copy for each object).
  4. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
  5. These variables will get memory as soon as we declared an object and inside the object.
  6. new operator provides memory to these variables.
  7. Instance variables can be declared in class level before or after use.
  8. Access modifiers can be given for instance variables.
  9. Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.
  10. Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class ( when instance variables are given accessibility) should be called using the fully qualified name . ObjectReference.VariableName.

Static variable

  1. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. It cannot be local.
  2. There will be only one copy of the static member variable irrespective of number of objects and this copy will be shared by all the objects.
  3. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value.
  4. Static variables are created when the program starts and destroyed when the program stops.
  5. Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks.
  6. These variable will get memory as soon as Jvm loads the class in memory, before creating any object.
  7. Static is a keyword.
  8. These variable can be accessed through class name as well as object name.
  9. Static variables are initialized when class is loaded.
  10. Static variables in a class are initialized before any static method of the class runs.
  11. These variables are having highest scope that is they can be accessed from any method/ block in a class.
  12. When declaring class variables as public static final, then variables names (constants) are all in upper case.

Example :

class A
	int x; //instance variable
	static int y;//static or class variable
class Variable
	public static void main(String args[])
		int n;//local variable
		A a1=new A();
		A a2=new A();
		A a3=new A();

output :

Previous Home Next