logo

Java의 정적 클래스

Java에서는 클래스를 다른 클래스 내에 정의할 수 있습니다. 이들은 호출됩니다 중첩 클래스 . 대부분의 개발자가 알고 있듯이 클래스는 정적일 수 있으므로 이제 일부 클래스는 Java에서 정적으로 만들 수 있습니다. 자바 지원 정적 인스턴스 변수 , 정적 메서드 , 정적 블록 및 정적 클래스. 중첩 클래스가 정의된 클래스를 외부 클래스 . 최상위 클래스와는 다르게 중첩 클래스는 정적일 수 있습니다. . 비정적 중첩 클래스는 다음과 같이 알려져 있습니다. 내부 클래스 .

메모: 최상위 클래스는 Java에서 정적일 수 없습니다. 정적 클래스를 생성하려면 중첩 클래스를 생성한 다음 이를 정적으로 만들어야 합니다.



내부 클래스의 인스턴스는 외부 클래스의 인스턴스 없이 생성될 수 없습니다. 따라서 내부 클래스 인스턴스는 외부 클래스 인스턴스에 대한 참조를 사용하지 않고도 외부 클래스의 모든 멤버에 액세스할 수 있습니다. 이러한 이유로 내부 클래스는 프로그램을 간단하고 간결하게 만드는 데 도움이 될 수 있습니다.

배우 제나트 아만

기억하다: 정적 클래스에서는 객체를 쉽게 생성할 수 있습니다.

정적 및 비정적 중첩 클래스의 차이점

다음은 정적 중첩 클래스와 내부 클래스 간의 주요 차이점입니다.



  1. 정적 중첩 클래스는 외부 클래스를 인스턴스화하지 않고도 인스턴스화될 수 있습니다.
  2. 내부 클래스는 외부 클래스의 정적 멤버와 비정적 멤버 모두에 액세스할 수 있습니다. 정적 클래스는 외부 클래스의 정적 멤버에만 액세스할 수 있습니다.

~의 정적 및 비정적 중첩 클래스

다음은 위에서 언급한 주제의 구현입니다.

문자열 날짜 변환

자바




// Java program to Demonstrate How to> // Implement Static and Non-static Classes> // Class 1> // Helper class> class> OuterClass {> >// Input string> >private> static> String msg =>'GeeksForGeeks'>;> >// Static nested class> >public> static> class> NestedStaticClass {> >// Only static members of Outer class> >// is directly accessible in nested> >// static class> >public> void> printMessage()> >{> >// Try making 'message' a non-static> >// variable, there will be compiler error> >System.out.println(> >'Message from nested static class: '> + msg);> >}> >}> >// Non-static nested class -> >// also called Inner class> >public> class> InnerClass {> >// Both static and non-static members> >// of Outer class are accessible in> >// this Inner class> >public> void> display()> >{> >// Print statement whenever this method is> >// called> >System.out.println(> >'Message from non-static nested class: '> >+ msg);> >}> >}> }> // Class 2> // Main class> class> GFG {> >// Main driver method> >public> static> void> main(String args[])> >{> >// Creating instance of nested Static class> >// inside main() method> >OuterClass.NestedStaticClass printer> >=>new> OuterClass.NestedStaticClass();> >// Calling non-static method of nested> >// static class> >printer.printMessage();> >// Note: In order to create instance of Inner class> >// we need an Outer class instance> >// Creating Outer class instance for creating> >// non-static nested class> >OuterClass outer =>new> OuterClass();> >OuterClass.InnerClass inner> >= outer.>new> InnerClass();> >// Calling non-static method of Inner class> >inner.display();> >// We can also combine above steps in one> >// step to create instance of Inner class> >OuterClass.InnerClass innerObject> >=>new> OuterClass().>new> InnerClass();> >// Similarly calling inner class defined method> >innerObject.display();> >}> }>

>

문자열을 정수로 변환하는 방법

>

산출

Message from nested static class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks>