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

¿Cómo crear una clase estática Object sin referenciar la clase externa en Java?

Static members (methods/A variable (field) belongs to the class and it 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 for all objects. You can use the static keyword to define a static field.

Ejemplo

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();
   }
}

Resultado de salida

Value of num in the main method 50
Valor de num en el método demo 50

Refer to the static member in the same class

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

Ejemplo

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);
   }
}

Resultado de salida

Valor de num en el método demo 50

Clase interna

En Java, puede contener clases dentro de una clase, que se conocen como clases internas.

Sintaxis

public class Outer{
   public class Inner{
   }
}

Cuando tiene una clase dentro de otra clase, actúa solo como miembro de instancia de la clase externa. Por lo tanto, si declara una clase interna estática, puede usar su nombre para acceder a sus miembros (clase interna)-

outer_class_name.inner_class_name.members

Ejemplo

class OuterDemo {
   static int data = 200;
   static class InnerDemo {
      public static void my_method() {
         System.out.println("Esta es mi clase anidada");
         System.out.println(OuterDemo.data);
      }
   }
}
public class StaticClassExample{
   public static void main(String args[]) {
      System.out.println(OuterDemo.data);
      //Outer.InnerDemo = new Outer.InnerDemo(); 
      OuterDemo.InnerDemo.my_method();
   }
}

Resultado de salida

200
Esta es mi clase anidada
200

Si intenta referirse a un miembro (estático) de una clase interna, no es necesario usar el nombre de la clase externa, también puede usar solo el nombre de la clase interna para referirse a los miembros.

Ejemplo

class OuterDemo {
   static int data = 200;
   static class InnerDemo {
      public static void my_method() {
         System.out.println("Esta es mi clase anidada");
         System.out.println(OuterDemo.data);
      }
   }
   public static void main(String args[]) {
      System.out.println(data);
      InnerDemo.my_method();
   }
}

Resultado de salida

200
Esta es mi clase anidada
200
Te gustará