산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> java

자바 중첩 정적 클래스

자바 중첩 정적 클래스

이 자습서에서는 예제를 통해 중첩된 정적 클래스에 대해 배웁니다. 또한 정적 클래스가 내부 클래스와 어떻게 다른지에 대해서도 배우게 됩니다.

이전 자습서에서 배운 것처럼 Java의 다른 클래스 내부에 클래스를 가질 수 있습니다. 이러한 클래스를 중첩 클래스라고 합니다. Java에서 중첩 클래스는 두 가지 유형이 있습니다.

우리는 이미 이전 튜토리얼에서 내부 클래스에 대해 논의했습니다. 내부 클래스에 대해 알아보려면 Java Nested Class를 방문하세요.

이 자습서에서는 중첩된 정적 클래스에 대해 배웁니다.

<시간>

자바 중첩 정적 클래스

static 키워드를 사용합니다. 중첩 클래스를 정적으로 만들기 위해.

참고: Java에서는 중첩된 클래스만 정적일 수 있습니다.

일반 클래스와 마찬가지로 정적 중첩 클래스에는 정적 및 비정적 필드와 메서드가 모두 포함될 수 있습니다. 예를 들어,

Class Animal {
   static class Mammal {
      // static and non-static members of Mammal
   }
   // members of Animal
} 

정적 중첩 클래스는 외부 클래스와 연결됩니다.

정적 중첩 클래스에 액세스하려면 외부 클래스의 개체가 필요하지 않습니다.

<시간>

예:정적 중첩 클래스

class Animal {

// inner class
   class Reptile {
      public void displayInfo() {
        System.out.println("I am a reptile.");
      }
   }

// static class
   static class Mammal {
      public void displayInfo() {
        System.out.println("I am a mammal.");
      }
   }
}

class Main {
   public static void main(String[] args) {
      // object creation of the outer class
      Animal animal = new Animal();

      // object creation of the non-static class
      Animal.Reptile reptile = animal.new Reptile();
      reptile.displayInfo();

      // object creation of the static nested class
      Animal.Mammal mammal = new Animal.Mammal();
      mammal.displayInfo();

   }
}

출력

I am a reptile.
I am a mammal.

위의 프로그램에는 두 개의 중첩된 클래스 Mammal이 있습니다. 및 파충류 동물 클래스 내부 .

비정적 클래스 Reptile의 객체를 생성하기 위해 다음을 사용했습니다.

Animal.Reptile reptile = animal.new Reptile()

정적 클래스 포유류의 개체를 만들려면 , 우리는

Animal.Mammal mammal = new Animal.Mammal()
<시간>

외부 클래스의 구성원 액세스

Java에서 정적 중첩 클래스는 외부 클래스와 연결됩니다. 이것이 정적 중첩 클래스가 외부 클래스의 클래스 멤버(정적 필드 및 메서드)에만 액세스할 수 있는 이유입니다.

외부 클래스의 비정적 필드와 메소드에 액세스하려고 하면 어떻게 되는지 봅시다.

예:비정적 멤버 액세스

class Animal {
  static class Mammal {
   public void displayInfo() {
     System.out.println("I am a mammal.");
   }
 }

 class Reptile {
   public void displayInfo() {
     System.out.println("I am a reptile.");
   }
 }

 public void eat() {
   System.out.println("I eat food.");
 }
}

class Main {
 public static void main(String[] args) {
   Animal animal = new Animal();
   Animal.Reptile reptile = animal.new Reptile();
   reptile.displayInfo();

   Animal.Mammal mammal = new Animal.Mammal();
   mammal.displayInfo();
   mammal.eat();
 }
}

출력

Main.java:28: error: cannot find symbol
    mammal.eat();
          ^
  symbol:   method eat()
  location: variable mammal of type Mammal
1 error
compiler exit status 1

위의 예에서는 비정적 메서드 eat()를 만들었습니다. Animal 클래스 내부 .

이제 eat()에 액세스하려고 하면 포유류 개체 사용 , 컴파일러에 오류가 표시됩니다.

포유류 때문입니다. 정적 클래스의 개체이며 정적 클래스에서 비정적 메서드에 액세스할 수 없습니다.

<시간>

정적 최상위 클래스

위에서 언급했듯이 중첩 클래스만 정적일 수 있습니다. 정적 최상위 클래스를 가질 수 없습니다.

최상위 클래스를 정적으로 만들려고 하면 어떻게 되는지 봅시다.

static class Animal {
 public static void displayInfo() {
   System.out.println("I am an animal");
 }
}

class Main {
 public static void main(String[] args) {
   Animal.displayInfo();
 }
}

출력

Main.java:1: error: modifier static not allowed here
static class Animal {
       ^
1 error
compiler exit status 1

위의 예에서는 정적 클래스 Animal을 만들려고 했습니다. . Java는 정적 최상위 클래스를 허용하지 않으므로 오류가 발생합니다.


java

  1. C# 정적 키워드
  2. C# 중첩 클래스
  3. 자바 최종 키워드
  4. 자바 instanceof 연산자
  5. Java 중첩 및 내부 클래스
  6. 자바 익명 클래스
  7. 자바 싱글톤 클래스
  8. 자바 리플렉션
  9. 자바 ObjectOutputStream 클래스
  10. 자바 제네릭