This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Static Members in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
The class level members which have static keyword in their definition are called static members.
Types of Static Members:
Java supports four types of static members
Static Variables
Static Blocks
Static Methods
Main Method (static method)
All static members are identified and get memory location at the time of class loading by default by JVM in Method area.
Only static variables get memory location, methods will not have separate memory location like variables.
Static Methods are just identified and can be accessed directly without object creation.
1.Static Variable:
A class level variable which has static keyword in its creation statement is called static variable.
package com.instanceofjava;
class Demo{
static int a=10;
static int b=20;
}
We can not declare local variables as static it leads to compile time error "illegal start of expression".
Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.
package com.instanceofjava;
class Demo{
static int a=10;
static int b=20;
public static void main(String [] args){
//local variables should not be static
static int a=10;// compile time error: illegal start of expression
}
All static variables are executed by JVM in the order of they defined from top to bottom.
JVM provides individual memory location to each static variable in method area only once in a class life time.
Life time and scope:
Static variable get life as soon as class is loaded into JVM and is available till class is removed from JVM or JVM is shutdown.
And its scope is class scope means it is accessible throughout the class.
package com.instanceofjava;
class StaticDemo{
static int a=10;
static int b=20;
public static void main(String [] args){
System.out.println("a="+a);
System.out.println("a="+b);
show();
}
public static void show(){
System.out.println("a="+a);
System.out.println("a="+b);
}
}
Duplicate Variables:
If multiple variables are created with same name are considered as duplicate variables.
In the same scope we can not create multiple variables with same name.
If we create it leads to compile time error "variable is already defined".
Even if we change data type or modifier or its assigned value we can not create another variable with same name.
package com.instanceofjava;
class StaticDemo
{
static int a=10;
int a=30;// Compile time error
}
But it is possible to create multiple variables with same name in different scopes.
package com.instanceofjava;
class StaticDemo
{
static int a=10;
int a=30;// Compile time error
public static void main(String [] args){
// it is allowed to define "a" in this method.
int a=20;
}
}
Shadowing:
It is possible to create local variables or parameters with same variable name.
The concept is called shadowing . It means local variable is a shadow of class level variable.
It means when you access it in side method , you will get local variables value but not from class level.
package com.instanceofjava;
class StaticDemo
{
static int a=10;
public static void main(String [] args){
int a=20;
System.out.println("a="+a): // prints 20
}
}
Local preference:
When we call a variable compiler and JVM will search for its definition in that method first, if its definition is not found in that method then will search for its definition at class level.
If its definition not found at class level also then compiler throws error: can not find symbol.
This phenomenon is called local preference.
package com.instanceofjava;
class StaticDemo
{
static int a=10;
public static void main(String [] args){
System.out.println("a="+a): // prints 10 : method level no definition so access class level
int a=20;
System.out.println("a="+a): // prints 20 :method level variable found
}
}
By using class name we can get class level variables value.
package com.instanceofjava;
class StaticDemo
{
static int a=10;
public static void main(String [] args){
System.out.println("a="+a): // prints 10 : method level no definition so access class level
int a=20;
System.out.println("a="+a): // prints 20 :method level variable found
System.out.println("a="+StaticDemo.a): // prints 10 : class level variable