[!INDEX]

  1. non sT m/t call using obj. in same class.
  2. non sT m/t call in static method w/o using obj in same class
  3. sT m/t call using cls. name and directly in same class
  4. different class method can't call directly (both static).
  5. different class method call directly using class name (both static).
  6. sT m/t can only access sT data ( int 1 = 10; ).
  7. sT m/t can't call sT m/t directly in same class.
  8. sT m/t Cannot Refer To 'This' Or 'Super' Keyword In Anyway.
  9. Only sT Block Executes Automatically When The Cls. Is Loaded In The Memory.
  10. Only sT Block + Main M/T sT Block Executes First Then Main m/t.
  11. Multiple sT Block In cls., Executes Top To Bottom, sT Members Executes First.

1. non sT m/t call using obj. in same class.

public class K02 {
  void display() {
    System.out.println("display method");
  }
  public static void main(String[] args) {
    K02 k1 = new K02();
    k1.display();
  }
}

2. non sT m/t call in static method w/o using obj in same class.

public class K02 {
  void display() {
    System.out.println("display method");
  }
  public static void main(String[] args) {
    display();
  }
} // non-static method display() cannot be referenced from a static context
  // display();

3 - sT m/t call using cls. name and directly in same class

public class K02 {
  static void display() {
    System.out.println("display method");
  }
  public static void main(String[] args) {
    display(); // works 
    K02.display(); // works 
    } 
} 
// display method 
// display method

4 - different class method can't call directly (both static).

public class K02 {
  static void display() {
    System.out.println("display method");
  }
  public static void main(String[] args) {
    pen();
  }
}
class Book {
  static void pen() {
    System.out.println("this is my pen");
  }
} // cannot find symbol pen();

5 - different class method call directly using class name (both static).

public class K02 {
  static void display() {
    System.out.println("display method");
  }
  public static void main(String[] args) {
    Book.pen();
  }
}
class Book {
  static void pen() {
    System.out.println("this is my pen");
  }
} // this is my pen

6 - sT m/t can only access sT data ( int 1 = 10; ).

public class K02 {
  int i = 10;
  static void display() {
    System.out.println(i);
  }
} 
// non-static variable i cannot be referenced from a static context //
// System.out.println(i);

[!important] In Java, static methods cannot directly access non-static instance variables because static methods belong to the class itself, not to any specific instance. Instance variables, on the other hand, are tied to individual objects of the class. Since i is an instance variable and display() is a static method, Java doesn't know which instance of the class the variable i belongs to


7 - sT m/t can't call sT m/t directly in same class.

public class K02 {
  int i = 10;
  static void display() {
    show();
    System.out.println("hi");
  }
  void show() {
    System.out.println("hello");
  }
} // non-static method show() cannot be referenced from a static context

8 - sT m/t Cannot Refer To 'This' Or 'Super' Keyword In Anyway.

public class K02 {
  int i = 10;
  static void display() {
    System.out.println(this.i); // can not use super also 
    } 
} 
// non-static variable this
// cannot be referenced from a static context

9 - Only sT Block Executes Automatically When The Cls. Is Loaded In The Memory.

public class K02 {
  static {
    System.out.println("hello");
  }
} // Main method not found in class K19, please define the main method as: //
  // public static void main(String[] args)

10 - Only sT Block + Main M/T sT Block Executes First Then Main m/t.

public class K02 {
  static { // 1 
      System.out.println("hello"); 
  } 
  public static void main(String[] args) { //2 
      System.out.println("hi...."); 
  } 
} 
// hello 
// hi....

11 - Multiple sT Block In cls., Executes Top To Bottom, sT Members Executes First.

public class K02 {
  static { // 1 
      System.out.println("static block first"); 
  } 
  public static void main(String[] args) { // 3 
      System.out.println("main block at last"); 
  } 
  static{ // 2 
      System.out.println("static block second");
    } 
}

// static block first 
// static block second 
// main block at last