Skip to main content

Understanding the static Keyword in Java


What is the static Keyword?

In Java, the static keyword is used to indicate that a particular member (variable or method) belongs to the class itself, rather than to instances of the class. This means that the static member can be accessed without creating an instance of the class.

Static Variables

A static variable is shared among all instances of a class. It is also known as a class variable.

Example:

public class Example { // static variable static int count = 0; Example() { count++; } public static void main(String[] args) { Example obj1 = new Example(); Example obj2 = new Example(); Example obj3 = new Example(); System.out.println("Count: " + Example.count); // Output: Count: 3 } }

In this example, the count variable is static and shared among all instances of the Example class. It keeps track of the number of instances created.

Static Methods

A static method belongs to the class rather than any particular object instance. You can call a static method without creating an object of the class.

Example:

public class Example { // static method static void displayMessage() { System.out.println("Hello, World!"); } public static void main(String[] args) { Example.displayMessage(); // Output: Hello, World! } }

In this example, the displayMessage method is static, so it can be called directly using the class name without creating an instance of the Example class.

Static Block

A static block is used for static initialization of a class. This block is executed when the class is first loaded into the memory.

Example:

public class Example { static int num; static String str; // static block static { num = 100; str = "Static Block Initialization"; } public static void main(String[] args) { System.out.println("Number: " + Example.num); // Output: Number: 100 System.out.println("String: " + Example.str); // Output: String: Static Block Initialization } }

In this example, the static block initializes the static variables num and str when the class is loaded.

Static Nested Class

A static nested class is a static class defined within another class. It can access the static members of the outer class.

public class OuterClass { static int data = 30; static class InnerClass { void display() { System.out.println("Data: " + data); } } public static void main(String[] args) { OuterClass.InnerClass inner = new OuterClass.InnerClass(); inner.display(); // Output: Data: 30 } }

In this example, InnerClass is a static nested class within OuterClass, and it can access the static variable data of the outer class.

Key Points to Remember

  1. Static variables and methods belong to the class and can be accessed without creating an instance of the class.
  2. Static methods can only directly access other static members (variables and methods).
  3. Static blocks are executed when the class is loaded and are used for static initialization.
  4. Static nested classes can access the static members of the outer class.
  5. The static keyword cannot be used with local variables inside methods

Comments

Popular posts from this blog

String Function

  package StringLearning; class Display { public static void print (String s) { System. out .println(s); } } public class StringFunc { public static void main (String[] args) { String s = "Java Programming" ; // Display original string Display. print (s); // Converts all characters to lower case Display. print ( "LowerCase: " + s.toLowerCase()); // Converts all characters to upper case Display. print ( "UpperCase: " + s.toUpperCase()); // Concatenates the specified string to the end of the original string Display. print ( "Concatenation: " + s.concat( " is easy." )); // Replaces each occurrence of a character with a new character Display. print ( "Replace 'a' with 'b': " + s.replace( 'a' , 'b' )); // Replaces each occurrence of a substring with a new substring Displ...

Understanding Access Modifier

Java Program package LAB_Report; /* 2. Write a program in Java to demonstrate the usage of access modifiers: public, private, protected and default. */ class AccessModifier{ int defaultValue ; // this is visible within the package only public int publicValue ; // this is visible everywhere protected int protectedValue ; // this is visible within the package and subclasses private int privateValue ; // this is visible within the class only public void setPrivateValue ( int privateValue) { this . privateValue = privateValue; } public int getPrivateValue () { return privateValue ; } } class AccessModifierProtected extends AccessModifier{ public void setValue ( int value2){ this . protectedValue = value2; } public int getValue2 (){ return protectedValue ; } } public class Lab_2 { public static void main (String[] args) { AccessModifier am = new AccessModifier(); ...

Understanding Objects, Classes, and Instance Variables in Programming

What is an Object? An object is an entity that possesses both state and behavior. In simple terms, an object is an instance of a class. It encapsulates the properties (state) and functionalities (behavior) defined by its class.  Example: Object: Pencil State:   Name: Ballpoint   Color: Black Behavior:   - To write  What is a Class? A class is a blueprint for creating objects. It defines a set of properties and methods that the created objects will have. Think of a class as a template that outlines what an object will be like. Components of a Class: Fields: Variables to store data. Methods: Functions to define behaviors. Constructors: Special methods to initialize objects. Blocks: Code blocks for initialization. Nested Class and Interface: Classes or interfaces defined within another class Instance Variables Instance variables are variables declared within a class but outside any method. They are specific to each object created from the class. Unlike static varia...