AppDividend
Latest Code Tutorials

Static Keyword in Java Tutorial | Java Static Keyword Example

0

Static Keyword in Java Tutorial | Java Static Keyword Example is today’s topic. The static keyword is used in Java, mainly for memory management. It is used with variables, methods, blocks, and nested class. It is a keyword that is used to share the same variable or method of a given class. This is used for a constant variable or a method that is the same for every instance of a class. The main method of a class is generally labeled static.

Static Keyword in Java Tutorial

Static is a keyword in java.  It is mainly used for memory management. While using a static variable or method, there is no need to create an object. Just use the class name before the static variable, method, block, or nested class.  Only one instance of the static member is created, which is shared across all the instances of the class. A static method cannot call the non-static method; it can only call the static method.

Static is the non-access modifier in Java which is applicable for the following:

  1. blocks
  2. variables
  3. methods
  4. nested classes

 

Static Keyword in Java Tutorial

Now we are going to discuss one by one all types of methods in which we can use it.

#Static Variable

  1. It is also known as a class variable.
  2. The static variable allocates memory only once in class area at the time of class loading, i.e., it saves memory.
  3. Static variable is used in real scenarios.
  4. Static variables can only be declared at a class level.
  5. Static fields can be accessed without the object initialization.

In Java, if the field is declared static, then precisely the single copy of that field is created and shared among all the instances of that class. It doesn’t matter how many times we initialize the class; there will always be only one copy of the static field belonging to it. The value of that static field will be shared across all the object of either the same or any different class.

From a memory perspective, static variables go in the particular pool in JVM memory called Metaspace (before Java 8, this pool was called as a Permanent Generation or PermGen, which was removed entirely and replaced with a Metaspace).

#Compelling Reasons to Use static Fields

  1. When the value of the variable is independent of objects.
  2. When a value is supposed to be shared across all the objects.

For Example the company name of employees, school name of students, etc. Name of the school is common for all students in that school.

#Syntax to proclaim static variable

public static name;

Let us understand it using the code given below.

class Variable 
{
	public static String name="Static Variable";
	public static void main(String [] args)
	{
		System.out.println(Variable.name);
	}
}

#Output

 

static variable in java

In the above code, we know how static variable is initialized (using static keyword) and accessed(using the class name before the static variable, e.g., Variable.name).

#Static Method in Java

  1. It is also known as the class method.
  2. The static method belongs to a class rather than the object of a class.
  3. Memory is allocated only once at a time of class loading, and a static method can be executed only once in the program.
  4. Instance methods can directly access both the instance methods and instance variables
  5. Instance methods can also access the static variables and static methods directly
  6. static methods can access all the static variables and other static methods
  7. The following combinations of an instance, class methods and variables are valid:
    1. Instance methods can directly access both instance methods and instance variables
    2. Instance methods can also access static variables and static methods directly
    3. Static methods can access all static variables and other static methods
    4. Static methods cannot access instance variables and instance methods directly; they need some object reference to do so.

When a method is declared with the static keyword, it is known as a static method. The most common example of the static method is the main( ) method. They can only directly call the other static methods. They can only directly access the static data. They cannot refer to this or super keyword in any way.

For example, the Factory design pattern (instance of a class can be created only once) provides good use of the static method. 

You can use the static method to create an instance of a class.

#Syntax to proclaim static methods

static void method1
{
	//relevant Codes
}

Let us understand it using the code given below.

class Main
{
	//static method declaring 
	static void m1()
	{
		System.out.println("I am Static Method");
	}
}
//driver class 
class Method
{
	public static void main(String args[])
	{
		Main.m1();  // static method calling 
	}
}

#Output

 

Static methods in Java

In the above code, we know how a static method is declared (static void m1() {}) and executed (using the class name, e.g., Main.m1()).

#Static Block

  1. It is used to initialize a static variable.
  2. A class can have multiple static blocks.
  3. It is a set of the statement, and JVM executes it before the execution of the main method (i.e., public static void main (String [] args)).

For example, When we want a statement to be executed before the main method. Static methods are used, and it is the same as the static variable for initialization of static variable or sometimes include a set of statements to be executed before the main method.

#Syntax to proclaim static block

static
{
    //Set of statements 
} 

Let us understand it using the code given below.

//creating a Block class 
class Block
{	
	//decalring static variables
	static int a=10;
	static int b;
	//decalring static block 
	static
	{
		System.out.println("i am Static block");
		b=a;
	}
	//main method 
	public static void main(String args[])
	{
		System.out.println(b);
	}
}

See the following output.

 

Static block in Java

In the above code, we understand how a static block is declared and executed without explicitly calling it. And how the logic is performed and then run before the execution of the main method.

#Static Nested Class

  1. Nested classes that are declared as static is called static nested class.
  2. The static nested classes only have access to static members of the outer class, including the private once.
  3. A static nested class is used in scenarios.

For Example when we do not want to create a mess of creating the instance of the outer class, and it is similar to the inner class, but the difference is that while creating the object or instance of the nested static inner class you don’t need an instance of the outer class.

#Syntax to proclaim static nested class

class A
{
    static class B
    {

    }
} 

Let us understand it using the code given below.

class Outer1
{  
   static int a=50;

   //static inner class declaring 
   static class Inner1
   {  
      void f()
      {
		System.out.println(a);
      }  
   }
   public static void main(String args[])
   {  
	 Outer1.Inner1 obj=new Outer1.Inner1();  
	 obj.f();  
   } 
}

See the following output.

 

static nested class

In the above code, we understand how nested inner class is defined and executed by creating the instance of the class (e.g., Outer1.Inner1 obj = new Outer1.Inner1()).

#When to use static variables and methods

Use a static variable for the property that is common to all the objects. For example, in the class Student, all students share the same college name. Use static methods for changing static variables.

#Conclusion

  1. Static is a keyword in java
  2. The Static method can be called using the class name no need to create the object of the class
  3. Static block is executed before a main method of the program
  4. Static variable and block are executed in the way they are present in the program.
  5. A class can have multiple static blocks.
  6. Memory is allocated only once at a time of loading.
  7. The static nested class cannot access non-static (instance) data member or method.

In short, Static keyword can be used with class, variable, method, and block. Static members belong to the class instead of a specific instance, and this means if you make a member static, you can access it without an object. 

Finally, Static Keyword in Java Tutorial | Java Static Keyword Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.