Local and Global Variables

Local Variables

  • Definition: A local variable is declared inside a method, constructor, or block and can only be accessed within that scope.
  • Lifetime: Exists only while the method/block is executing.
  • Example:
public class Example {
      void display() {
          int localVar = 10; // Local variable
          System.out.println(localVar);
      }
  }

Global (Instance) Variables

  • Definition: A variable declared inside a class but outside any method, constructor, or block.
  • Lifetime: Exists as long as the object exists.
  • Example:
public class Example {
      int globalVar = 20; // Global variable
      void display() {
          System.out.println(globalVar);
      }
  }

Using Static and Non-Static Elements

1. Static to Static

  • Direct Access: Static variables, methods, and objects can be accessed inside static methods without creating an object.
  • Example:
class Test {
      static int num = 10;
      static void display() {
          System.out.println(num);
      }
      public static void main(String[] args) {
          display();
      }
  }

2. Non-Static to Static

  • Requires Object: A non-static variable/method must be accessed through an instance inside a static method.
  • Example:
class Test {
      int num = 20;
      void display() {
          System.out.println(num);
      }
      public static void main(String[] args) {
          Test obj = new Test(); // Creating object
          obj.display(); // Accessing non-static method
      }
  }

3. Static to Non-Static

  • Direct Access: A non-static method can directly access a static variable/method.
  • Example:
class Test {
      static int num = 30;
      void display() {
          System.out.println(num); // Direct access
      }
  }

4. Non-Static to Non-Static

  • Direct Access: Non-static variables/methods can be accessed inside non-static methods directly.
  • Example:
class Test {
      int num = 40;
      void display() {
          System.out.println(num); // Direct access
      }
  }

Naming Rules

Class Names

  • Start with an uppercase letter.
  • Use CamelCase.
  • Example: StudentDetails, EmployeeData

Variable and Object Names

  • Start with a lowercase letter.
  • Use camelCase.
  • Example: studentAge, employeeSalary

Method Names

  • Use verbs.
  • Start with lowercase.
  • Example: getName(), calculateSalary()

Comments in Java

Single-line Comment

// This is a single-line comment

Multi-line Comment

/*
 This is a multi-line comment.
 Used to describe more information.
*/

Methods vs Constructors

Methods

  • Used to define behavior.
  • Can return values.
  • Need to be called explicitly.
  • Example:
class Test {
      void display() {
          System.out.println("Hello");
      }
  }

Constructors

  • Used to initialize objects.
  • No return type.
  • Automatically called when an object is created.
  • Example:
class Test {
      Test() {
          System.out.println("Constructor Called");
      }
  }

This blog covers all aspects of Java local/global variables, static vs non-static elements, naming rules, comments, constructors, methods, and object creation in depth.