Java 기초 (접근 제어자, 캡슐화, Wrapper 클래스)

KimGwangmin·2026년 9월 4일

접근 제어자

대상이 어디까지 접근할 수 있는지를 정하는 키워드이다.

접근제어자같은 클래스같은 패키지자식 클래스모든 곳
publicOOOO
protectedOOO△
(default)OOXX
privateOXXX

캡슐화

OOP의 핵심 개념 중 하나
접근 제어자를 통해 외부에서 객체 내부를 함부로 변경하지 못하게 보호할 수 있다

public class Student {
    private String name;
    private int age;
    private int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public void printInfo() {
        System.out.println(name + " / " + age + "세 / " + score + "점");
    }
}

이 Student 클래스는 모든 필드가 private이므로, 한 번 생성된 객체에 대해 외부에서 필드 값을 수정하지 못한다.

Getter, Setter

  • Getter: private 필드의 값을 외부에서 조회할 수 있게 해주는 메서드
  • Setter: private 필드의 값을 외부에서 수정할 수 있게 해주는 메서드

Wrapper Class

자료형을 객체로 감싸는 클래스이다.

기본 자료형 (Primitive Type)래퍼 클래스 (Wrapper Class)
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

기본 자료형은 속성, 메서드를 가질 수 없다.
Wrapper 클래스를 통해 감싸면 여러 기능을 제공할 수 있다.

예시

// 가능
Integer num1 = 123;
String str1 = num.toString();

// 불가능
int num2 = 100;
String str2 = a.toString();

0개의 댓글