Start Coding Now

Static Keyword in Java - Memory Management

Understand the static keyword in Java. Static variables, methods, blocks, and nested classes explained.

Static Keyword

The static keyword is used for memory management mainly. It applies to variables, methods, blocks, and nested classes.

Static Variable: Gets memory only once in the class area at the time of class loading. Shared among all objects.

Static Method: Belongs to the class rather than the object of a class. Can be invoked without creating an instance of a class.

class Student {
   int rollno;
   String name;
   static String college = "ITS"; // static variable
   
   static void change() { // static method
     college = "BBDIT";
   }
}

Frequently Asked Questions