Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, August 26, 2013

Static in Java - Variables, Methods, Class and Static blocks

Static in Java - Variables, Methods, Class and Static blocks


Static is a keyword is applied to the following in Java.

1) Variable
2) Method
3) Class
4) Static blocks

Static Variables :



Static variables have special feature in Java as this initialized and executed only once before class instantiated. Static variables are comes under class level and no need to create the object to invoke static variables. Directly can be accessed by its class name. This is being shared by all objects created by its class.
Refer below Example program covered all scenarios for Static Variable:

Example 1:

package istav;
class StaticClass{
public static String name;

public static String getName() {
return name;
}

public static void setName(String name) {
StaticClass.name = name;
}
}

public class StaticEx {

public static void main(String[] args) {

StaticClass class1 = new  StaticClass();
class1.setName("Static name 1");

StaticClass class2 = new  StaticClass();
class2.setName("Static name 2");

System.out.println("class1 :: "+class1.getName());
System.out.println("class2 :: "+class2.getName());

StaticClass.name = "Static Name 3";
StaticClass.name = "Static Name 4";

System.out.println(StaticClass.name);
}
}

Output :

class1 :: Static name 2
class2 :: Static name 2
Static Name 4


Static Methods :

Static methods are similar to the static variables as to access static methods, no need of object to be invoked.


Example :



package w3schools.statics;

class StaticMethod{

public  static void display(String method)
{
System.out.println("static method :: "+method);
}
}

public class StaticMethods {

public static void main(String[] args) {

StaticMethod.display("One");

}

}


Output :

static method :: One


Static Class :


Static class can be applied to only Inner classes. In inner nested static class can acess only outer class static variable and static methods. Nested static inner class can not access any non static variable and methods.


Static class Example 1 :



package w3schools.statics;

class Outer{

static String name = "java";

static class Inner{

public void display()
{
System.out.println("nested Inner Static Class Method");
displayOuter();
}
}

public static void displayOuter(){
System.out.println("Outer display method");
}

}

public class StaticClassEx {

public static void main(String[] args) {


Outer.Inner inner = new Outer.Inner();

inner.display();
}

}



Output :

nested Inner Static Class Method
Outer display method

Static class Example 2: 



package w3schools.statics;

class Outer{

static String name = "java";

static class Inner{

public static void display()
{
System.out.println("nested Inner Static Class Method");
displayOuter();
}
}

public static void displayOuter(){
System.out.println("Outer display method");
}

}

public class StaticClassEx {

public static void main(String[] args) {

Outer.Inner.display();
}

}

Output :

nested Inner Static Class Method
Outer display method


Static Blocks:

Static blocks will be executed before main method executes and load the class onto JVM.

Example 1:


package w3schools.statics;

public class StaticBlocksEx {

public static void main(String[] args) {

System.out.println("Main method Starts");

System.out.println("Main method Ends");

}

static{
System.out.println("StaticBlocksEx static block");
}

}


Output :

StaticBlocksEx static block
Main method Starts
Main method Ends

Example 2:



package w3schools.statics;

class A{

static{
System.out.println("Static A block");
}
}

class B extends A{

static{
System.out.println("Static B block");
}
}

public class StaticBlocksEx extends B{

public static void main(String[] args) {

System.out.println("Main method Starts");

// A a = new B();
StaticBlocksEx blocksEx = new StaticBlocksEx();

System.out.println("Main method Ends");

}

static{
System.out.println("StaticBlocksEx static block");
}

}


Output :

Static A block
Static B block
StaticBlocksEx static block
Main method Starts
Main method Ends

If we create the object to subclass than first will looks for static methods in the super classes and proceeding by subclasses.

1 comment:

Please do not add any spam links in the comments section.