In this article Static keyword in Java we give the information about static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes.

Static Keyword in Java:

In Java, static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes.

We know that the class is inherited. That is properties of one class can be inherited of property of another class. Same way using the static keyword we can share the variables and methods of a class. To create a static member, we have to declare it with static keyword.

When a member of a class is declared with the ‘static’ keyword, it can be accessed before the objects of the class are created and without any object reference.

In Java programming language, static keyword is a non-access modifier and can be used for the following:-

  1. block
  2. Variable
  3. Method
  4. Class

Some important points of static variables:-

  •  We can create static variables only at class level.
  • Static block and static variable are executed in the same order in which they are written in the program.
  • These are also called class variables.
  • The main advantage of static variable is that it saves memory.

Static variable in Java:

When we declare a variable as static, only a single copy of the variable is created and it is shared with all the objects at the class level.

The static variables are global variables. All instances of a class share the same static variable.

// static variable program with output

class Employee

{

          int empid;

          String name;

          static String company=”TCS”;

          Employee(int empid, String name)

          {

                   this.empid=empid;

                   this.name=name;

          }

          void display()

          {

                   System.out.println(empid+” “+name+” “+company);

          }

}

class StaticVariable

{

          public static void main(String []args)

          {

                   Employee p=new Employee(101,”Ram”);

                   p.display();

                   Employee q=new Employee(102,”Geeta”);

                   q.display();

          }

}

// Output:

101 Ram TCS

102 Geeta TCS

Static Method in Java:

When a method is declared with static keyword then it is called static method. The most common example of a static method is the main() method. Methods declared with static have the following restrictions:-

  • Static method belong to class not object.
  • Static methods can be call directly by class name and does not need any object.
  • A static method can access only static data. it cannot access non-static data.
  • They can only call other static methods directly.
  • They can directly access static data members and change their values.
  • These cannot be referred to as this or super in any way.

// They can directly access static data members and change their values.

class StaticDemo
{
static int n=100;         // data member
public static void display()            // static function
{
System.out.println(“Value of N: “+n);
}
public static void main(String []args)
{
StaticDemo.display();
}
}

Output:

Value of N: 100

// They can only call other static methods directly.

class StaticMethod
{
public static void display()
{
show();
System.out.println(“Hello!”);
}
public static void show()
{
System.out.println(“Hi!”);
}
public static void main(String []args)
{
StaticMethod.display();
}
}

// Output

Hi!

Hello!

//These cannot be referred to as this or super in any way.

class StaticThis
{
int n=100;
public static void show()
{
System.out.println(this.n);
}
public static void main(String []args)
{
StaticThis.show();
}
}

//Output

Compile time Error

StaticThis.java:6: non-static variable this cannot be referenced from a static context
System.out.println(this.n);

// static method program with output

class Area
{
void RectArea()
{
int l=5,b=4;
System.out.println(“Area of rectangle: “+(l*b));
}
public static void AreaSquare()
{
int s=7;
System.out.println(“Area of Square: “+(s*s));
}
}
public class AllArea
{
public static void main(String []args)
{
Area obj=new Area();
// call the nonstatic method using object
obj.RectArea();
// call the static method using class name
Area.AreaSquare();
}
}

Output:

Area of rectangle: 20
Area of Square: 49

Static block in Java:

  • Static block execute automatically when the class is loaded in the memory.

// Program:

public class StaticBlockExample
{
// Static block
static
{
System.out.println(“Static block is executed before main method.”);
}

// Main method
public static void main(String[] args)
{
System.out.println(“Main method is executed.”);
}
}

// Output:

Static block is executed before main method.
Main method is executed.

static Class java program:

In Java, you can declare a static nested class, which is a class defined inside another class with the static keyword.

public class OuterClass
{
// Static nested class
static class StaticNestedClass
{
void display()
{
System.out.println(“Inside static nested class.”);
}
}
public static void main(String[] args)
{
// Creating an object of the static nested class
OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();
obj.display();
}
}

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *