• The static keyword can be used with class, variable, method and block.
  • Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without an object.
Static Block
  • A static block is used for initializing the static variables. This block gets executed when the class is loaded in the memory.
  • A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
class JavaExample2{
static int num;
static String mystr;
//First Static block
static{
System.out.println("Static Block 1");
num = 68;
mystr = "Block1";
}
//Second static block
static{
System.out.println("Static Block 2");
num = 98;
mystr = "Block2";
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
System.out.println("Value of mystr: "+mystr);
}
}

Output:

Static Block 1
Static Block 2
Value of num: 98
Value of mystr: Block2
Static Variables
  • A single copy of static variable is created and shared among all the instances of the class.
  • Memory allocation for such variables only happens once when the class is loaded in the memory.
class JavaExample{
//Static integer variable
static int var1=77;
//non-static string variable
String var2;

public static void main(String args[])
{
JavaExample ob1 = new JavaExample();
JavaExample ob2 = new JavaExample();
ob1.var1=88;
ob1.var2="I'm Object1";
ob2.var1=99;
ob2.var2="I'm Object2";
System.out.println("ob1 integer:"+ob1.var1);
System.out.println("ob1 String:"+ob1.var2);
System.out.println("ob2 integer:"+ob2.var1);
System.out.println("ob2 STring:"+ob2.var2);
}
}

Output:

ob1 integer:99
ob1 String:I'm Object1
ob2 integer:99
ob2 STring:I'm Object2
Static Methods
  • Static Methods can access class variables(static variables) without using object(instance) of the class.
  • Syntax: static return_type method_name();

class JavaExample{
static int i = 100;
static String s = "Beginnersbook";
//Static method
static void display()
{
System.out.println("i:"+i);
System.out.println("i:"+s);
}

//non-static method
void funcn()
{
//Static method called in non-static method
display();
}
//static method
public static void main(String args[])
{
JavaExample obj = new JavaExample();
//You need to have object to call this non-static method
obj.funcn();

//Static method called in another static method
display();
}
}


Output:

i:100
i:Beginnersbook
i:100
i:Beginnersbook
Static Class
  • A class can be made static only if it is a nested class.
  • Nested static class doesn’t need a reference of Outer class.
  • A static class cannot access non-static members of the Outer class.
class JavaExample{
private static String str = "BeginnersBook";

//Static class
static class MyNestedClass{
//non-static method
public void disp() {

/* If you make the str variable of outer class
* non-static then you will get compilation error
* because: a nested static class cannot access non-
* static members of the outer class.
*/
System.out.println(str);
}

}
public static void main(String args[])
{
/* To create instance of nested class we didn't need the outer
* class instance but for a regular nested class you would need
* to create an instance of outer class first
*/
JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass();
obj.disp();
}
}
Output:

BeginnersBook
Important Points:
  • All the static member variables are kept on the Permanent Generation area
  • Code inside a static block is executed only once: the first time you make an object of that class or the first time you access a static member of that class.
  • Static blocks are executed before constructors.
  • Static Block is also called before Instance Initialization Block
  • This example will help you to understand the above 3 points.
class Test {

static int i;
int j;

// Initializer block starts..
{
// This code is executed before every constructor.
System.out.println("Initializer blocker called");
}

// Static block
static {
i = 10;
System.out.println("static block called ");
}

// Constructor Block
Test() {
System.out.println("Constructor called");
}
}

public class StaticTest {
public static void main(String args[]) {
System.out.println(Test.i);
Test t1 = new Test();
Test t2 = new Test();
}
}


OUTPUT:

static block called
10
Initializer blocker called
Constructor called
Initializer blocker called
Constructor called

 

Categories: JAVA

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *