English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Как создать статический класс Object без ссылки на внешний класс Java?

Static members (methods/variables) belong to the class and will be loaded into memory along with the class. You can call it without creating an object (use the class name as a reference). There is only one copy of a static field available throughout the class, that is, the value of a static field is the same in all objects. You can use the static keyword to define a static field.

Пример

public class Sample{
   static int num = 50;
   public static void demo(){
      System.out.println("Value of num in the demo method " + Sample.num);
   }
}
public class Demo{
   public static void main(String args[]){
      System.out.println("Value of num in the main method " + Sample.num);
      Sample.demo();
   }
}

Результат вывода

Value of num in the main method 50
Значение num в методе demo 50

Refer to the static member of the same class

If you want to refer to a class's own static member (within the same class), you do not need to refer to the class itself; you can directly access the static member.

Пример

public class Sample{
   static int num = 50;
   public static void demo(){
      System.out.println("Value of num in the demo method " + Sample.num);
   }
   public static void main(String args[]){
      demo();
      System.out.println(num);
   }
}

Результат вывода

Значение num в методе demo 50

Внутренний класс

В Java вы можете включать классы в классах, которые называются внутренними классами.

Грамматика

public class Outer{
   public class Inner{
   }
}

Когда у вас есть класс в другом классе, он играет роль только экземплярного члена внешнего класса. Поэтому, если вы объявляете внутренний класс статическим, вы можете использовать его имя для доступа к его членам (внутренний класс) -

outer_class_name.inner_class_name.members

Пример

class OuterDemo {
   static int data = 200;
   static class InnerDemo {
      public static void my_method() {
         System.out.println("Это мой вложенный класс");
         System.out.println(OuterDemo.data);
      }
   }
}
public class StaticClassExample{
   public static void main(String args[]) {
      System.out.println(OuterDemo.data);
      // Вложенный Outer.InnerDemo = новый Outer.InnerDemo(); 
      OuterDemo.InnerDemo.my_method();
   }
}

Результат вывода

200
Это мой вложенный класс
200

Если вы пытаетесь ссылаться на статический член внутреннего класса (статический), вам не нужно использовать имя внешнего класса, вы можете ссылаться на член только по имени внутреннего класса.

Пример

class OuterDemo {
   static int data = 200;
   static class InnerDemo {
      public static void my_method() {
         System.out.println("Это мой вложенный класс");
         System.out.println(OuterDemo.data);
      }
   }
   public static void main(String args[]) {
      System.out.println(data);
      InnerDemo.my_method();
   }
}

Результат вывода

200
Это мой вложенный класс
200
Вам может понравиться