What is Static Keyword in java?

 Static Keyword in java

Static keyword is basically used for the memory management purpose in java.

Static keyword belongs to class and can be apply with the following

1. Variable

2. Method

3. Block

4. Nested class

1. Java Static Variable: If you declare any variable with static keyword then it is called static Variable

  • The static variable gets memory once in the class area at the time of class loading
  • we can create static variable at class level. 
  • static keyword can be used with class name without creating instance

Example of java static variable

public class StaticKeywordDemo {

	static int a=10;
	
	public static void main(String h[])
	{
        System.out.println(StaticKeywordDemo.a);//className.variable-name
	}
}

2. Java Static Method: If you declare any method with static keyword then it is called static method

  • static keyword can be used with class name without creating instance

  • static method belong to class rather then object of class

public class StaticKeywordDemo {

static public void display() //method declaration
{
System.out.println("Hello static method");
}
public static void main(String h[])
{

StaticKeywordDemo.display();// method called with class name

}
}

3. Java Static Block: 

  • It initialize before the static data member.
  • Static block executed before the main method at the time of class loading in java
public class StaticKeywordDemo {
static { System.out.println("Hello static block"); } public static void main(String h[]) { System.out.println("hello java "); } }

4. Java Static Nested Class: A static class i.e. created inside a class is called static nested class in java. It cannot access non-static data members and methods. 

  • It can be accessed by outer class name.It can access static data members of outer class including private.
  • Static nested class cannot access non-static (instance) data member or method

Example of static nested class: if all data member in outer class are static

class TestOuter{  
	  static int data=30;  
	  static class Inner{  
	   void msg(){System.out.println("data is "+data);}  
	  }  
	  public static void main(String args[]){  
	  TestOuter.Inner obj=new TestOuter.Inner();  
	  obj.msg();  
	  }  
	}  

Example of static nested class: if all data member in outer class & inner class are static

class TestOuter{

static int data=30; static class Inner{ static void msg(){System.out.println("data is "+data);} } public static void main(String args[]){ TestOuter.Inner.msg();//no need to create the instance of static nested class } }

        Comments

        Popular posts from this blog

        org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver