[!INDEX]

  1. Definition
  2. Use
  3. Can't declare static Var. In M/t.
  4. sT Var. Can't Directly Access In Another cls.( without cls name)
  5. sT Var. Can Directly Access In Another cls. withs its cls. Name
  6. Non sT Var. Can Not Access In Another sT cls. Using cls. Name
  7. employee obj. init. ( id,name,comp) using c/t , w/o using sT
  8. employee obj. init. ( id,name,comp) using c/t , with static keyword.
  9. counter program using instance Var..
  10. counter program using sT Var..

1. Definition

[!NOTE]

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

2. Use

[!NOTE]

  1. can be used for counting the number of object created
  2. The count variable is for class.
  3. The static variable can be used to refer to the common property or value for all objects, for example, company name of employees, college name of students, etc.
  4. static m/T belongs to the class not object.
  5. used for memory management.
  6. "static" methods belongs to the class, not to the object.
  7. A "static" method can be accessed directly by class name and does'nt need any object.
  8. A "static" method can access only static data. It cannot access non-static data (instance data).
  9. A "static" method can call only other static methods and cannot call a non-static method.
  10. A "static" method cannot refer to "this" or "super" keyword in anyway.
  11. can print hello without main method in static block upto version 1.6 java
  12. static block is executed at class loading, hence at the time of class loading if we want to perform any activity, we have to define that inside static block.
  13. static block is used to initialize the static members

3. Can't declare static Var. In M/t.

class K02 { 
    int a =10; static int b = 20; void m1(){ 
        static int c =30; 
    } 
    public static void main(String[] args) { 
    // other code
    } 
}

4. sT Var. Can't Directly Access In Another cls.( without cls name)

public class K02 {
    static int a = 20;  // Static variable
}
class Book {
    public static void main(String[] args) {
        System.out.println(a);  // Accessing static variable 'a' using the class name
    }
}

5. sT Var. Can Directly Access In Another cls. withs its cls. Name

public class K02 {
    static int a = 20;  // Static variable
}
class Book {
    public static void main(String[] args) {
        System.out.println(K02.a);  // Accessing static variable 'a' using the class name
    }
}

6. Non sT Var. Can Not Access In Another sT cls. Using cls. Name.

public class K02 { 
    int a = 20; 
    } 
class book { 
    public static void main(String[] args) { 
    System.out.println(K02.a); 
    } 
}

7. employee obj. init. ( id,name,comp) using c/t , w/o using sT

public class K02 {
  public static void main(String[] args) {
    Employee e1 = new Employee(101, "Rajat", "kb");
    e1.display();
    Employee e2 = new Employee(102, "Deepak", "kb");
    e2.display();
    Employee e3 = new Employee(103, "Manoj", "kb");
    e3.display();
  }
}
class Employee {
  int empid;
  String emp_name;
  String emp_comp;
  Employee(int empid, String emp_name, String emp_comp) {
    this.empid = empid;
    this.emp_name = emp_name;
    this.emp_comp = emp_comp;
  }
  void display() {
    System.out.println(empid + " " + emp_comp + " " + emp_name);
  }
} 
// 101 kb Rajat 
// 102 kb Deepak 
// 103 kb Manoj

8. employee obj. init. ( id,name,comp) using c/t , with static keyword.

public class K02 {  
  public static void main(String[] args) {
    Employee e1 = new Employee(101, "Rajat");
    e1.display();
    Employee e2 = new Employee(102, "Deepak");
    e2.display();
    Employee e3 = new Employee(103, "Manoj");
    e3.display();
  }
}
class Employee {
  int empid;
  String emp_name;
  static String emp_comp = "kb";
  Employee(int empid, String emp_name) {
    this.empid = empid;
    this.emp_name = emp_name;
  }
  void display() {
    System.out.println(empid + " " + emp_comp + " " + emp_name);
  }
} 
// 101 kb Rajat 
// 102 kb Deepak 
// 103 kb Manoj

9. counter program using instance Var..

public class K02 {
  int count = 0;
  K02() {
    count++;
    System.out.println(count);
  }
  public static void main(String[] args) {
    K02 k1 = new K02(); // 1 
    K02 k2 = new K02(); // 1 
    K02 k3 = new K02(); // 1
    K02 k4 = new K02(); // 1 
    } 
} 
// The static variable
// gets memory only once in the 
// class area at the time of class loading.

10. counter program using sT Var..

public class K02 {
  static int count = 0;
  K02() {
    count++;
    System.out.println(count);
  }
  public static void main(String[] args) {
    K02 k1 = new K02(); // 1 
    K02 k2 = new K02(); // 2 
    K02 k3 = new K02(); // 3
    K02 k4 = new K02(); // 4 
    } 
}