Static

Static

  • It is used for memory management.
  • It can be applied to variable, method, inner class and static block.
  • It means single copy storage.

Static variable-

  • A variable which is defined with static keyword called as “static variable.”
  • This is also known as class variables.
  • It is stored into heap area.
  • It is used to refer the common property of all the objects.
  • Static variables get loaded into memory at the time of class loading.

How to declare and initialize static variable.

Example-

static int a; //Declaration of static variable

static int b=10; //Initialization of static variable

How to access static variable.

There are two ways to access the static variables.

  • 1. By using class name
  • 2. By using object name.

Program for static variables within same class

package com.test;

public class StaticDemo {

static int a = 10;

public static void main(String[] args) {

StaticDemo staticDemo = new StaticDemo();

System.out.println("value of a>>" + a); // direct if its in same class only

System.out.println("using class name>>" + StaticDemo.a); // by using class name

System.out.println("using object name>>" + staticDemo.a); // by using object name

}

}

Output-

value of a>>10

using class name>>10

using object name>>10

Program for static variables within different class

package com.test;

public class StaticDemo {

static int a = 10;

}

package com.test;

public class Demo {

public static void main(String[] args) {

StaticDemo staticDemo = new StaticDemo();

System.out.println("using class name>>" + StaticDemo.a); // by using class name

System.out.println("using object name>>" + staticDemo.a); // by using object name

}

}

Output-

using class name>>10

using object name>>10

Why it is called as single copy storage?

Note- Static members get loaded into memory as soon as (StaticDemo staticDemo1) read this line. Non static members gets loaded into memory after reading (new StaticDemo())- this line.

package com.test;

public class StaticDemo {

int a = 5;

static int b = 5;

public static void main(String args[]) {

StaticDemo staticDemo1 = new StaticDemo();

System.out.println("non static>>" + staticDemo1.a++);

System.out.println("static>>" + staticDemo1.b++);

StaticDemo staticDemo2 = new StaticDemo();

System.out.println("non static>>" + staticDemo2.a++);

System.out.println("static>>" + staticDemo2.b++);

StaticDemo staticDemo3 = new StaticDemo();

System.out.println("non static>>" + staticDemo3.a++);

System.out.println("static>>" + staticDemo3.b++);

StaticDemo staticDemo4 = new StaticDemo();

System.out.println("non static>>" + staticDemo4.a++);

System.out.println("static>>" + staticDemo4.b++);

}

}

Output-

non static>>5

static>>5

non static>>5

static>>6

non static>>5

static>>7

non static>>5

static>>8

How to access static members from non-static members.

package com.test;

public class StaticDemo {

void x2() {

System.out.println("This is non static method");

x1(); // calling static method from non static method.

}

static void x1() {

System.out.println("This is static method");

}

public static void main(String args[]) {

StaticDemo staticDemo = new StaticDemo();

staticDemo.x2();

}

}

Output-

This is non static method.

This is static method.

Static method-

  • If you define any method with static keyword then it is called as static method.
  • It is known as class method.
  • It belongs to class rather than object of class.
  • It loads into memory before object creation.
  • It can access only static data member only.
  • package com.test;

public class StaticDemo {

static void x1() {

System.out.println("This is static method");

}

public static void main(String args[]) {

StaticDemo.x1();

}

}

Output-

This is static method.

Static block-

  • It is group of statements that are executed when class is loading into memory by Classloader.
  • It is widely used to create the static resource.
  • We cannot access non-static variable into static block.
  • It is always executed first.

Example-1

public class Test {

static {

System.out.println("this is the 1st static block...");

}

public static void main(String[] args) {

System.out.println("this is main method..");

}

}

Here, we will get first output is “This is 1st static block” because it is executed first than main method.

Output-

this is the 1st static block...

this is main method..

Example-2

public class StaticExample {

static {

System.out.println("this is the 1st static block...");

}

static {

System.out.println("this is the 2nd static block...");

}

static {

System.out.println("this is the 3rd static block...");

}

public static void main(String[] args) {

System.out.println("this is main method..");

}

}

output

this is the 1st static block...

this is the 2nd static block...

this is the 3rd static block...

this is main method..

Note-

1. Local variables cannot be static.

2. We cannot call non-static member from static member because static variables stored into memory before object creation and non-static variables stored into memory after object creation.

3. Outer class cannot be static but inner class can be static.

4. Constructor cannot be static.

5. Main method is static method.

6. This and super keywords are not allowed inside the static method or static area.