`

1. Constructors in Java

A constructor is a special method used to initialize objects in Java. It is automatically called when an object is created.

Key Features:

Types of Constructors:

  1. Default Constructor (No-Argument Constructor):
    • Provided automatically by Java if no constructor is defined.
    • Initializes the object with default values.
    • Example:
      class Example {
          int num;
          String str;
      
          // Default Constructor
          Example() {
              num = 0;
              str = "Default";
          }
      }
      
  2. Parameterized Constructor:
    • Accepts arguments to initialize object fields with specific values.
    • Example:
      class Example {
          int num;
          String str;
      
          // Parameterized Constructor
          Example(int num, String str) {
              this.num = num;
              this.str = str;
          }
      }
      
  3. Copy Constructor:
    • Used to create a copy of an object.
    • Not built-in, but you can define it manually.
    • Example:
      class Example {
          int num;
          String str;
      
          // Copy Constructor
          Example(Example other) {
              this.num = other.num;
              this.str = other.str;
          }
      }
      
  4. Private Constructor:
    • Used to restrict object creation from outside the class.
    • Commonly used in Singleton Design Pattern.
    • Example:
      class Singleton {
          private static Singleton instance;
      
          // Private Constructor
          private Singleton() {}
      
          public static Singleton getInstance() {
              if (instance == null) {
                  instance = new Singleton();
              }
              return instance;
          }
      }
      

2. Static Keyword in Java

The static keyword is used for memory management and defines members that belong to the class rather than to individual instances.

Where static Can Be Used:

  1. Static Variables:
    • Shared among all instances of a class.
    • Example:
      class Example {
          static int count = 0;
      
          Example() {
              count++;
          }
      }
      
  2. Static Methods:
    • Can be called without creating an object.
    • Cannot access non-static members directly.
    • Example:
      class Example {
          static void displayMessage() {
              System.out.println("Static Method");
          }
      }
      
  3. Static Blocks:
    • Used for static initialization.
    • Executed once when the class is loaded.
    • Example:
      class Example {
          static {
              System.out.println("Static Block Executed");
          }
      }
      
  4. Static Nested Classes:
    • A nested class declared static can be accessed without an instance of the outer class.
    • Example:
      class Outer {
          static class Nested {
              void display() {
                  System.out.println("Static Nested Class");
              }
          }
      }
      

3. Why Is the Main Method Static?

The main method is static because the JVM needs to invoke it without creating an instance of the class. Since the program execution starts with the main method, it must be accessible without requiring an object.

Main Method Syntax:

public static void main(String[] args) {
    // Program execution starts here
}

Reasons:

  1. Direct Access: JVM does not need to create an object of the class to invoke the main method.
  2. Memory Efficiency: Static methods are memory-efficient and do not require object creation.
  3. Program Entry Point: It ensures the main method can be called as the starting point of the program.

4. Static Classes in Java

A static class in Java refers to a static nested class. It is a nested class that is declared static within an outer class.

Characteristics:

Example:

class Outer {
    static class StaticNested {
        void display() {
            System.out.println("Static Nested Class");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.StaticNested nested = new Outer.StaticNested();
        nested.display();
    }
}

Use Cases:


5. Types of Constructors in Detail

a. Default Constructor

b. Parameterized Constructor

c. Copy Constructor

d. Private Constructor