Static Class, Block, Methods and Variables in Java

Static keywords can be used with class, variable, method and block. Static members are related to class instead of any particular instance, that means if you make a member static, then you can access it without object. Let us take an example to understand this:-

Static Method

Here we have a static method myMethod(), we can call this method without any object. If we remove static keyword and make it non-static then we have to make it an object of class to call it.

Static member is common to all instances of the class, but is different for each instance of non-static member class.
class SimpleStaticExample
,
// This is a static method
static void myMethod()
,
System.out.println(“myMethod”);
,

public static void main(String[] args)
,
/* You can see that we are calling this
* method without creating any object.
,
myMethod();
,
,

Result (Output):
myMethod

Static Block

Static block is used to initialize the static variable, this block gets executed when the class is loaded in the memory. A class can have many static blocks, which will be executed in the same order in which they are written in the program.

Example 1:- single static block
As you can see that both the static variables are initialized before they are accessed in the main method.

also read

class JavaExample{
static int num;
static String mystr;
static{
num = 97;
mystr = “Static keyword in Java”;
,
public static void main(String args[],
,
System.out.println(“Value of num: “+num);
System.out.println(“Value of mystr: “+mystr);
,
,

Result (Output):
Value of number: 97
Value of mystr: Static keyword in Java

Multiple static block

Let us see how many static blocks work in java. They are executed in a particular order –

First the first static block is executed, after that the second static block is executed, that is why the value initialized by the first block is overwritten by the second block.

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);
,
,

Result (Output):
Static Block 1
Static Block 2
Value of number: 98
Value of mystr: Block2

Static Variable

A static variable is common to all the instances of the class because static variables are class level variables. In other words, it can be said that only one copy of the static variable is created and shared among all the instances of the class. The memory allocation for such variables happens only once when the class is loaded into the memory.

Some important points :-

  • Static variable is also known as class variable.
  • Unlike non-static variables, such variables can be directly accessed in static and non-static ways.

Example 1

The static variable can be accessed directly from the static method. Here we have a static method disp() and two static variables var1 and var2.

class JavaExample3{
static int var1;
static String var2;
//This is a Static Method
static void disp(){
System.out.println(“Var1 is: “+var1));
System.out.println(“Var2 is: “+var2);
,
public static void main(String args[],
,
disp();
,
,
Result (Output):
Var1 is: 0
Var2 is: null

Example 2 :

Static variables are shared among all the instances of the class. In this example the string variable is non-static and the integer variable is static. As you can see in the output that the non-static variable is different for both the objects but the static variable is shared among themselves, that is the reason that the changes done by the object obj2 in the static variable to both the object will affect

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();
//Assigning the value to static variable using object ob1
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);
,
,

Result (Output):
ob1 integer:99
ob1 String:I’m Object1
ob2 integer:99
ob2 STring:I’m Object2

static method

Static methods can access class variables (static variables) without using class objects, while non-static methods and non-static variables are accessed using class objects. Static methods can be accessed directly. Can be accessed through static and non-static methods.

syntax:
static return_type method_name();

Example 1

In the given example the static method main() is accessing the static variable without using the object.

class JavaExample{
static int i = 10;
static String s = “ComputerHindiNotes”;
//This is a static method
public static void main(String args[],
,
System.out.println(“i:”+i);
System.out.println(“s:”+s);
,
,
Result (Output):
i:10
s:ComputerHindiNotes

Example 2

In the given example static method is accessed in static and non-static method.

class JavaExample{
static int i = 100;
static String s = “ComputerHindiNotes”;
//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();
,
,

Result (Output):
i:100
i:ComputerHindiNotes
i:100
i:ComputerHindiNotes

static class

  1. A class can be made static only if it is a nested class.
  2. A nested static class does not require a reference to any other class.
  3. A static class cannot access non-static members of another class.

Example: Static class

class JavaExample{
private static String str = “ComputerHindiNotes”;

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

System.out.println(str);
,

,
public static void main(String args[],
,
JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass();
obj.disp();
,
,
Result (Output) :
ComputerHindiNotes

,