1. 클래스란?

  • 자바 프로그램 구성의 기본 단위이다
  • 객체(object)를 생성하기 위하여 정의한 설계도(blueprint) 또는 틀(frame)이다
  • 객체(object)는 클래스(class)에 의해 정의되고, 클래스에 의해 설계된 내용을 생성한 결과물이다
  • 자바는 객체지향 언어이기 때문에 모든 코드를 클래스 내에서 작성한다
  • 객체의 구조를 클래스 내에서 코드로 정의하면 이후에 동일한 구조의 클래스는 쉽게 생성할 수 있다
  • 객체의 상태는 필드(데이터)로, 행위는 메소드로 구현된다
  • 객체는 특정 클래스의 인스턴스이다
  • 클래스는 자체로 타입으로 사용할 수 있으며 연관된 기능들을 묶을 수 있다

  ※ 인스턴스(instance)

  • 클래스를 통해 생성된 객체를 말한다
  • 클래스로부터 객체를 만드는 과정을 '인스턴스화(instantiate)라고 한다
  • 객체와 인스턴스는 비슷한 맥락의 의미를 가지지만 객체가 메모리에 할당되어 구현될 때를 인스턴스로 볼 수 있다

제품과 클래스 구분 예시

2. 클래스 구성

 1) 필드(field)

  • 클래스의 속성(state)을 나타내는 변수
  • 모델명, 컬러, 바퀴의 수 등등...

 2) 메서드(method)

  • 클래스의 기능(behavior)을 나타내는 함수
  • 시동걸기, 가속하기, 정지하기 등등...

 3) 생성자(constructor)

  • 클래스의 객체를 생성하는 역할

 4) 이너 클래스(inner class)

  • 클래스 내부의 클래스를 의미

※ 필드, 메서드, 이너클래스 3요소를 클래스의 멤버(member)라고 부른다

 

 

※ 참조 링크

▶ 해시넷 클래스 : http://wiki.hash.kr/index.php/%ED%81%B4%EB%9E%98%EC%8A%A4

 

클래스 - 해시넷

클래스(class)란 객체 지향 프로그래밍(OOP)에서 새로운 객체(object)를 만들기 위한 템플릿을 말한다. 클래스를 사용하여 인스턴스(instance)를 생성함으로써 객체를 만들 수 있다. 파일 확장자는 .class

wiki.hash.kr

위키피아 클래스 : https://en.wikipedia.org/wiki/Class_(computer_programming) 

 

Class (computer programming) - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Definition in programming that specifies how an object works In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial valu

en.wikipedia.org

 

 

3. 내부 클래스(Inner Class)

  • 클래스 내에 선언된 클래스이다
  • 외부 클래스와 내부 클래스가 서로 연관되어 있을 때 사용한다
  • 내부 클래스를 사용하면 외부 클래스의 멤버들에 쉽게 접근 할 수 있고, 코드의 복잡성을 줄일 수 있다
  • 외부적으로 불필요한 데이터를 감출 수 있어 캡슐화(encapsulation)를 달성하는 데 유용하다
  • 기본적으로 내부 클래스는 외부 클래스 내에 선언된다는 점을 제외하면 일반 클래스와 차이점이 없다
    - 외부 클래스와 내부 클래스가 서로 연관되어 있을 때 사용의 편의성을 고려하여 만들어진 문법 요소이다
class Outer { // 외부 클래스
	
	class Inner {
		// 인스턴스 내부 클래스	
	}
	
	static class StaticInner {
		// 정적 내부 클래스
	}

	void run() {
		class LocalInner {
		// 지역 내부 클래스
		}
	}
}
  • 내부 클래스의 종류는 세가지로 구분한다
    - 인스턴스 내부 클래스
    - 정적 내부 클래스
    - 지역 내부 클래스
  • 세 가지 내부 클래스는 변수가 선언 위치에 따라 인스턴스 변수, 클래스 변수, 그리고 지역 변수로 구분되는 것과 유사하게 그 위치를 중심으로 구분될 수 있다
  • 각각의 유효범위(scope)와 특성이 변수의 구분과 매우 유사하다
  • 내부클래스 종류
종 류 선언 위치 사용 가능한 변수
인스턴스 내부 클래스
(instance inner class)
외부 클래스의 멤버변수 선언위치에 선언(멤버 내부 클래스) 외부 인스턴스 변수
외부 전역 변수
정적 내부 클래스
(static inner class)
외부 클래스의 멤버변수 선언위치에 선언(멤버 내부 클래스) 외부 전역 변수
지역 내부 클래스
(local inner class)
외부 클래스의 메서드나 초기화블럭 안에 선언 외부 인스턴스 변수
외부 전역 변수
익명 내부 클래스
(anonymous inner class)
클래스의 선언과 객체의 생성을 동시에 하는 일회용 익명 클래스 외부 인스턴스 변수
외부 전역 변수

 

 1) 인스턴스 내부 클래스(instance inner class)

  • 인스턴스 내부 클래스는 외부 클래스의 내부에 위치해 있다
  • private 접근 제어자(해당 클래스 안에서만 접근 가능한 멤버에 사용)를 사용하고 있음에도 내부에서 외부 클래스의 인스턴스 변수와 정적 변수에 각각 접근하여 해당 값을 사용하고 있다
  • 인스턴스 내부 클래스는 반드시 외부 클래스를 생성한 이후에 사용해야 한다
  • 클래스의 생성과 상관없이 사용할 수 있는 정적 변수와 정적 메서드는 인스턴스 내부 클래스에서 선언할 수 없다
class Outer{
    private int num = 1;
    private static int sNum = 2;
    private InnerClass innerClass;
    public Outer(){
       innerClass = new InnerClass();
    }

    class InnerClass{
        int inNum = 10;
        void innerMethod(){
            System.out.println("Outer num = " + num + "(instance variable of outer class)");
            System.out.println("Outer sNum = " + sNum + "(static variable of outer class");
        }
    }
    public void testClass(){
        innerClass.innerMethod();
    }
}

public class MainClass {
    public static void main(String[] args) {
        Outer outer = new Outer();
        System.out.println("call up inner class function used outer class");
        outer.testClass();
    }
}

 

 2) 정적 내부 클래스

  • 내부 클래스가 외부 클래스의 존재와 무관하게 정적 변수를 사용할 수 있는 것이 정적 내부 클래스이다
  • 인스턴스 내부 클래스와 동일하게 클래스의 멤버 변수 위치에 정의하지만 static 키워드를 사용한다
class Outer1{
    private int num = 3;
    private static int sNum = 4;
    void getPrint(){
        System.out.println("Instance Method");
    }
    static void getPrintStatic(){
        System.out.println("Static Method");
    }

    static class StaticInClass {
        void test() {
            System.out.println("Outer num = " + sNum + "(static variable of outer class)");
            getPrintStatic();
        }
    }
}
public class StaticInnerClass {
    public static void main(String[] args) {
        Outer1.StaticInClass a = new Outer1.StaticInClass();
        a.test();
    }
}

 

 

4. 지역 내부 클래스(local inner class)

  • 지역 내부 클래스는 클래스의 멤버가 아닌 메서드 내에서 정의되는 클래스이다
  • 지역 변수와 유사하게 메서드 내부에서만 사용가능하다
  • 일반적으로 메서드 안에서 선언 후에 바로 객체를 생성해서 사용한다
class Outer2{
    int num = 5;
    void test(){
        int num2 = 6;
        class LocalInClass{
            void getPrint(){
                System.out.println(num);
                System.out.println(num2);
            }
        }
        LocalInClass localInClass = new LocalInClass();
        localInClass.getPrint();
    }
}
public class LocalInnerClass {
    public static void main(String[] args) {
        Outer2 outer = new Outer2();
        outer.test();
    }
}

  • 지역 내부 클래스 LocalInClass가 메서드 안에서 선언되고 생성된 후에 정의된 메서드를 호출하여 외부 클래스의 변수들을 출력하고 있다
  • 내부 클래스는 기본적으로 개발자의 편의를 위해 서로 연관있는 클래스들을 연결시켜 주는 것이다

 

'JAVA' 카테고리의 다른 글

Java - 타입(Type)  (0) 2022.05.19
Java - 개요3. 파일 생성  (0) 2022.05.19
Java - 개요2. JVM & JDK  (0) 2022.05.18
Java - 개요1. 정의  (0) 2022.05.18
OOP - this  (0) 2022.05.13

+ Recent posts