Static Keyword in Java with Example – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:00:06 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Static Keyword in Java with Example – Programmerbay https://programmerbay.com 32 32 Static Keyword in Java with Example https://programmerbay.com/what-are-the-use-of-static-keyword-in-java/ https://programmerbay.com/what-are-the-use-of-static-keyword-in-java/#respond Tue, 25 Aug 2020 08:49:12 +0000 https://www.programmerbay.com/?p=4784 What is a static keyword in Java?

static keyword in javaJava provides a special type of member which doesn’t depend on the instance of a class.

These members execute independently without using class instance.

To create such members Java supports a static keyword that allows them to get executed before object creation.

A Static keyword can be imposed on both methods and variables.

The main method is a classic example of a static keyword that doesn’t need to the object for execution instead it runs before an instance is created.

How to access static variable or method ?

To access a static variable or to invoke a static method dot operator is used with the class name to which that particular variable or method belongs.

Points to remember:

  • Only a single copy is created and used in case of static variable
  • An object is not needed to access static variable and method
  • Run before the creation of an object
  • We can’t declare constructors as static
  • A class cannot be declared as static, though Java allows an inner class to be defined as static
  • Using static with the final keyword on a variable, create constant

Static keyword can be used with:

  1. Variables
  2. Methods
  3. Blocks or initializers
  4. Inner class
  • Java Static Variable

When a variable is declared with the static keyword, it acts like a global variable.

In other words, All the objects share the same variable which gets a single copy in memory, unlike instance variable which gets memory each time when a new object is made.

Static variables initialized with the same default values as instance variables do.

Syntax:

static dataType variableName;
  • Java Static method with example

When a method is preceded with a static keyword then it can only access static variables and call the static method right away.

It doesn’t allow this or super keyword as they are references of superclass or subclass.

A static method can be invoked using the class name.

For example, className.static_method(). static methods cannot be overridden.

Syntax:

static return_type method_name(){ 
//body 
}

Java program to show the working of static method:

public class Test
{				//Static variable 
  static int y;
    Test ()
  {
    System.out.println ("Hi!I am constructor");
  }				// Static method to access static variable 
  static void example ()
  {
    y = 10;
    System.out.println ("Hi! I am static and can access static variable =" +
      y);
  }
  public static void main (String[]args)
  {
    Test obj = new Test ();
    Test.example ();
  }

}

Output:

Hi!I am constructor
Hi! I am static and can access static variable = 10
  • Java Static block with example

When a pair of curly braces is followed by a static keyword, then it is referred to as static initializer or static initialization block.

It executes only once for respective class, rather then for every object.

As the name suggests it is used to initialize static data members only. These types of blocks don’t have return statement and can’t throw checked exceptions.

Syntax:

static{ 
//static variable initializer 
}

Java program to show the working of static block:

public class Test
{
  //Static variable 
  static int y;
    Test ()
  {
    System.out.println ("Hi!I am constructor");
  }				// Static block to initialize variable 
  static
  {
    y = 10;
    System.out.println ("I am static Initializer = " + y);
  }
  public static void main (String[]args)
  {
    Test obj = new Test ();
  }

}

Output:

I am static Initializer = 10
Hi!I am constructor
  • Java Static Nested Class with example

There is a concept of nested class which can be defined as a class when declared within another class is known as a nested class and when this nested class is preceded with a static keyword then it is said to be a static nested class.

In other words, a static nested class is a static member of the outer class.

It doesn’t require to wait for the instantiation of an object of the outer class which means we can create an object of static inner class independently.

  • A static nested class can have static or nonstatic members.
  • A static member of an outer class can be accessed by the methods of static inner class whereas non-static can’t be accessed directly. However, it can be possible by using the reference of the outer class.

Syntax:

class A
{
  static class B
  {
      
  }

}

Java program to show the working of static nested class:

public class Test
{

  //Static variable 
  static int y;
  // constructor 
    Test ()
  {
    System.out.println ("Hi!I am constructor");
  } static class nestedClass
  {
    void display ()
    {
      System.out.println ("I am from static nested class");
    }

  }
  public static void main (String[]args)
  {
    Test obj = new Test ();
    Test.nestedClass o = new Test.nestedClass ();
    o.display ();
  }

}

Output:

Hi!I am constructor
I am from static nested class

Java Program to demonstrate Working of static keyword

Program:

class Test
{

  static int y;			//Static variable 

    Test ()			// constructor 
  {
    System.out.println ("Hi!I am constructor");
  }

  static class nestedClass	//Nested static class 
  {
    void display ()
    {
      System.out.println ("I am from static nested class");
    }

  }

  // Static block to initialize variable static

  {
    y = 10;
    System.out.println ("I am static Initializer = " + y);
  }

  static void example ()	// Static method to access static variable 
  {
    y = 10;
    System.out.println ("Hi! I am static and can access static variable =" +
      y);
  }
  public static void main (String[]args)
  {
    Test obj = new Test ();

    Test.nestedClass o = new Test.nestedClass ();	// object of inner class 
    Test.example ();
    o.display ();
  }

}

Output:

Working of static keyword in Java with example output

]]>
https://programmerbay.com/what-are-the-use-of-static-keyword-in-java/feed/ 0