Static Keyword in Java
Things to remember:
- Static is a keyword used for memory management.
- Static means single copy storage for variable or method.
- Static keyword can be applied to variables, methods, inner class and blocks.
- Static members belongs to class rather than instance of class.
Static in java is a keyword that indicates that the variables or functions are shared between all the instances of a particular class since it belongs to the type, not the object. It is used when the programmer wants to share the same variable or method of a class.
Static Variable
The variable preceded by ‘static’ keyword is ‘static variable’static int a=10; // variable
static void m1(){ // method
}Static variable is used to refer common property of all objects of class.
How to access static variable?
There are two ways to access static variable:Static variable can be accessed by Class name
A. a ; [A is class name]
Where A is the class name and ‘a’ is a static variable declared in that classStatic variable can be accessed by object
I have a class name called ‘Sample’. Now, we can create the object of the Sample class
Sample h=new Sample (); System.out.println(h.a); //’a’ is static variable inside ‘sample’ class
How can I access static variable in two ways?
We will see in the following program:
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class Staticvar
{
static int i=10;
public static void main(String[] args)
{
Staticvar s =new Staticvar();
System.out.println(s.i); //Not Recommended
System.out.println(Staticvar.i); //Recommended
}
}
Output:
10
10
In the above program, we printed the value of variable by using object name and by using class name
Static variable gets loaded into the memory at the time of class loading
So, we can access static variable by reference variables as well.
In the above program, if we create only the reference of class like
The above example compiles and executes successfully because the static variable get loaded into the memory at the time of class loading.Staticvar s1=null; System.out.println(s1.i); // possible System.out.println(Staticvar.i); //possibleStatic variable and method doesn't belong to Object/Instance of the class since static variables are shared across all the instances of Object
Example 1
package com.jbk;
/*
* @author Java By Kiran
*/
public class StaticVar_Demo{
int a =10;
static int b =10;
public static void main(String[]args){
StaticVar_Demo st= new StaticVar_Demo();
System.out.println(st.a);
System.out.println(st.b);
StaticVar_Demo st1 = new StaticVar_Demo();
int x = st1.a++;
System.out.println(x);
int y = st1.b++;
System.out.println(y);
StaticVar_Demo st2 = new StaticVar_Demo();
int p = st2.a++;
System.out.println(p);
int q = st2.b++;
System.out.println(q);
StaticVar_Demo st3 = new StaticVar_Demo();
int c = st3.a++;
System.out.println(c);
int d = st3.b++;
System.out.println(d);
}
}
Output:
10
10
10
10
10
11
10
12
Example 2
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class Staticvar {
static int i=10;
int b=20;
void m1(){
System.out.println(i);
}
static void m2(){
System.out.println(i);
}
public static void main(String[] args) {
Staticvar s =new Staticvar();
s.m1();
Staticvar s1=new Staticvar();
s1.m2();
}
}
Output:
10
10
Local variables cannot be declared as static else the compiler displays a modifier error [at compile time]
We cannot call non-static members from static members because static variables get stored into memory before object creation and non-static member get stored into memory after object creation
So, when we access a non-static member through a static member, it leads to a compile time error as they are not present in the memory.
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class Staticvar {
static int i = 10; // static variable
int b = 20; // non static variable
void display() { // non static method
static int a = 50; //here a compile time error shows
System.out.println(i);
}
static void show() { // static method
System.out.println(i); // OK
System.out.println(b);
//if I want access non static variable in static method,
//compiler throw error
}
public static void main(String[] args) {
Staticvar s1 = new Staticvar();
s1.show();
s1.display();
}
}
Static Method
- If you apply static keyword with any method, it will be treated as static method
- Static method belongs to a class rather than object of a class
- Static method can be invoked without creating instance of the class
- Static method can access static data member and can change its value
- Static methods also load into memory before object creation
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class Staticvar {
static int i=10;
int b=20;
void display(){
System.out.println(i);
}
static void show(){ //static method
System.out.println(i);
System.out.println(b);//if we want access non static
//variable in static method, compiler throw error
}
public static void main(String[] args){
Staticvar s1=new Staticvar();
s1.show();
s1.display();
}
}
Static method can be accessed by nullable reference like
Staticvar s1=null; s1.show();By using non-static member we can access static members, but by using static member we cannot access non-static members
Example
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class StaticMethod {
static int i=10; //static variable
int b=20; //global variable
void display()
{
System.out.println("This is display method");
}
static void show() { //static method
System.out.println("Hello,This is JAVA BY KIRAN classes");
}
public static void main(String[] args)
{
StaticMethod s =new StaticMethod();
s.display();
s.show();
StaticMethod s1 = null;
s1.show();
}
}
Output:
This is display method
Hello, This is JAVA BY KIRAN classes
Hello, This is JAVA BY KIRAN classes
Static Block
- Java's static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.
- It is used to initialize static variables of the class. Mostly it's used to create static resources when the class is loaded
- We can't access non-static variables in static block
- We can have multiple static blocks in a class, although it doesn't make much sense
- Static block code is executed only once when the class is loaded into memory
- Static block always get executed first because they get stored into the memory at the time of class loading or before object creation
Example
package Static;
public class StaticBolckLab2 {
StaticBolckLab2()
{
System.out.println("This is constructor");
}
{
System.out.println("This is Non static Block");
}
static
{
System.out.println("This is static Block");
}
public static void main(String[] args) {
StaticBolckLab2 s= new StaticBolckLab2();
StaticBolckLab2 s1=new StaticBolckLab2();
}
Output:
This is static Block
This is Non staticBlock
This is constructor
This is Non static Block
This is constructor
- In the above example, we can prove that static members have one copy storage
- A static block cannot access non-static variables and methods
Example
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class StaticBlock {
static int i=10; //static variable
int b=20; //global variable
void display(){
System.out.println("This is display method");
}
static void show(){ //static method
System.out.println("JAVA BY KIRAN classes Pune");
//System.out.println(b); //if we want access variable ‘b’,non-static method is needed
//if there is a variable in the static method, JVM throws a compile time error
}
{
display();
}
static{
display(); //this is a compile time error, we cannot call non-static methods
System.out.println(i); // no error
System.out.println(" "+b); //this compile time error shows we cannot access non-static //variable in static block
}
public static void main(String[] args) {
StaticBlock s =new StaticBlock();
s.display();
s.show();
StaticBlock s1 = null;
s1.show();
}
}
Static Inner Class
- Outer class cannot be declared as static
- But inner classes can be used as static
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class StaticInnerclass {
static class A { //start of static inner class
static int a=10;
int b=20;
void display(){
System.out.println("This is local method");
}
static void show(){
int c=63;
System.out.println("This is static method");
}
}
public static void main(String[] args){
A a =new A();
System.out.println(a.b);
System.out.println(a.a);
a.display();
a.show();
}
}
Output:
20
10
This is local method
This is static method
- Inner class can access their local variables and methods
Example
package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
class StaticClassDemo {
static class JBKway{ //start of inner class
static int a=10;
int b=20;
void display(){
System.out.println("This is local method");
}
static void show(){
int c=63;
System.out.println("This is static method");
System.out.println("C="+c);
}
}
public static void main(String[]args) {
JBKway a1 =null;
//System.out.println(a1.b);//runtime error
System.out.println(a1.a);
// a1.display(); //runtime error
a1.show();
System.out.println(a1.a);
}
}
Output:
10
This is static method
C=63
10
Note:
- Local variable cannot be static
- Constructor cannot be static
- Outer class cannot be static