✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

Inner Class

inner class instance 생성 방법과 주의해야할 점!

  • Inner Class Type and Scope

: Same as variable

class Outer {
    class InstanceInner {}
    static class StaticInner {}
    
    void myMethod() {
        class LocalInner {}
    }
}

Inner Class

Description

Instance Class (인스턴스 클래스)

외부 클래스와 멤버 변수 선언 위치에 선언.

외부 클래스의 인스턴스멤버처럼 다루어진다.

주로 외부 클래스의 인스턴스멤버들과 관련된 작업에 사용될 목적으로 선언된다.

static class 사용가능하다.

Static Class (스태틱 클래스)

외부 클래스의 멤버변수 선언 위치에 선언하며, 외부 클래스의 static 멤버처럼 다루어진다.

주로 외부 클래스의 static 멤버, 특히 static 메서드에서 사용될 목적으로 선언된다.

변수와 마찬가지로, static class는 instance class를 사용하지 못한다!

왜냐하면, instance 클래스는 객체가 생성되고나서 생성되는데, static class는 객체 생성 없이 가능한데 static class에서 사용하는 시점에 객체가 생성됐는지 알 수 없기 때문이다.

Local Class (지역 클래스)

외부 클래스의 메서드나 초기화블럭 안에 선언하며, 선언된 영역 내부에서만 사용될 수 있다.

Anonymous Class (익명 클래스)

클래스의 선언과 객체의 생성을 동시에 하는 이름없는 클래스(일회용)

주로 이벤트 처리에 사용된다.

like 익명함수

  • Inner Class modifier and scope

  1. 외부 클래스의 private 멤버도 접근가능하다.

  2. 외부 클래스의 지역변수는 final이 붙은 변수만 접근가능하다. (JDK 1.8부터 에러가 나진 않지만, 의식적으로 final을 붙여서 사용하도록 하자.)

If you need static members in inner class, then make it static inner class.

Only static inner class can have static members.

class Ex7_12 {
    class InstanceInner {
        int iv = 100;
        //static int cv = 100;error
        final static int CONST = 100;//여기서 final static은 상수이므로 허용
    }
    //only static inner class can have static members
    static class StaticInner {
        int iv = 200;
        static int cv = 200;
    }
    
    void myMethod() {
        class LocalInner {
            int iv = 300;
            //static int cv = 300;error
            final static int CONST = 300;
        }
    }
}
  • Creating inner class : inner class is kind of instance member, so only after creating outer class instance, it can be used. Static inner class instance is possible without creating outer class.

class Ex7_15 {
    public static void main(String[] args) {
        //1.Creating outer class instance
        Outer2 oc = new Outer2();
        //2.Creating inner class instance
        Outer2.InstanceInner ii = oc.new InstanceInner();
        
        System.out.println("Outer2.StaticInner.cv : " +Outer2.StaticInner.cv); 
    }
}
PreviousDefault method and static methodNextAnonymous Class

Last updated 4 years ago

Was this helpful?