Static Keyword in Java

Introduction to Static Keyword in Java

The `static` keyword in Java is used to declare class members that belong to the class itself, rather than to instances of the class. This means that static members can be accessed without creating an instance of the class, and they are shared by all instances of the class.

Static members can include variables, methods, and nested classes. Static variables are also known as class variables, and they are stored in the class's memory space, rather than in the memory space of individual instances of the class. Static methods are also known as class methods, and they can be called without creating an instance of the class.

Static members are useful for a variety of purposes, such as:

  • Declaring constants
  • Defining utility methods that can be used by all instances of the class
  • Creating helper classes that are specific to a particular class

Here is an example of how to use the `static` keyword:


  public class MyClass {
    private static int count = 0;
  
    public static int getCount() {
      return count;
    }
  
    public static void incrementCount() {
      count++;
    }
  
    public static void main(String[] args) {
      System.out.println(MyClass.getCount()); // Prints 0
  
      MyClass.incrementCount();
  
      System.out.println(MyClass.getCount()); // Prints 1
    }
  }
 

In this example, the `count` variable is declared as static. This means that it belongs to the `MyClass` class itself, rather than to instances of the `MyClass` class.

The `getCount()` and `incrementCount()` methods are also declared as static. This means that they can be called without creating an instance of the `MyClass` class.

The `main()` method creates a new instance of the `MyClass` class and then calls the `getCount()` and `incrementCount()` methods. The output of the program is:

  0
  1

The `static` keyword is a powerful feature of Java that can be used to write more efficient and reusable code. By understanding how to use the `static` keyword, you can write code that is easier to maintain and understand.

Exercises

Code for counting the number of objects created for a class using a static variable


package morningsession;
import java.util.*;
import univ.*;

public class first 
{
  public static void main(String[] args) 
  {
    students s1 = new students();
    students s2 = new students();
    System.out.println(students.count);			
    }
}

public class students {
  private String name;
  private String regno;
  //using a static variable to count the objects
  public static int count=0;
  
  public students()
  {
    count++;
  }
  
  public void addinfo()
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter name");
    name=input.nextLine();
    System.out.println("Enter regno");
    regno=input.nextLine();
  }
  
  public void viewinfo()
  {
    System.out.println(name+regno);
  }
} 

Code for sorting an array of objects within a static method.


  package morningsession;
  import java.util.*;
  
  public class first 
  {
    public static void main(String[] args) 
    {
      students s[] = new students[3];
      for(int i=0;i<3;i++)
      {
        s[i]=new students();
      }
      for(int i=0;i<3;i++)
      {
        s[i].addinfo();
      }
      students.sortobjects(s);
      }
  }
  
  class students {
    private String name;
    private String regno;
    private int marks;
    
    public static void sortobjects(students s[])
    {
      students s2=new students();
      for(int i=0;i<s.length-1;i++)
      {
        for(int j=0;j<s.length-i-1;j++)
        {
          if(s[j].marks>s[j+1].marks)
          {
            s2=s[j];
            s[j]=s[j+1];
            s[j+1]=s2;
          }
        }
        
      }
      for(int i=0;i<3;i++)
      {
        s[i].viewinfo();
      }
    }
    
    public void addinfo()
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter name");
      name=input.nextLine();
      System.out.println("Enter regno");
      regno=input.nextLine();
      Scanner input1 = new Scanner(System.in);
      System.out.print("Enter marks");
      marks = input1.nextInt();
    }
    
    public void viewinfo()
    {
      System.out.println(name+regno+marks);
    }
  }