Code&Data Insights

[ Java Programming ] Inner Class 본문

Computer Science/Java Programming

[ Java Programming ] Inner Class

paka_corn 2022. 4. 17. 12:13

[ Inner Class ]

: Inner classes are classes defined within other classes

- The class that includes the inner class is called the outer class

There is no particular location where the definition of the inner class (or classes) must be placed within the outer class

- Placing it first or last, however, will guarantee that it is easy to find

 

An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members.

- An inner class is local to the outer class definition

- The name of an inner class may be reused for something else outside the outer class definition

- If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class.

 

Inner class를 사용하는 이점 

1) They can make the outer class more self-contained since they are defined inside a class

 코드의 복잡성을 줄일 수 있다

 

2) Both of their methods have access to each other's private methods and instance variables

내부 클래스에서 외부 클래스의 멤버들을 쉽게 접근할 수 있다

* Inner and Outer classes have access to each other's private members

< Static Inner Class >

  • A normal inner class has a connection between its objects and the outer class object that created the inner class object- This allows an inner class definition to reference an instance variable, or invoke a method of the outer class
  • There are certain situations, however, when an inner class must be static- If an object of the inner class is created within a static method of the outer class- If the inner class must have static members
Comments