Java/[인프런] 김영한의 실전 자바 - 기본편

[Java] 클래스의 멤버 메서드 종류 : static 메서드, 인스턴스 메서드

annovation 2025. 5. 31. 23:51

static 메서드

💡static 메서드란?

  • static 키워드로 선언된 메서드는 클래스에 속하며, 특정 인스턴스에 속하지 않습니다. 따라서 객체를 생성하지 않고도 클래스 이름을 통해 직접 호출할 수 있습니다. [각주:1]
"Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class."
  • 정적 메서드, 클래스 메서드라고도 부릅니다.

static 메서드 사용 방법

  • static 메서드는 클래스 이름을 통해 호출합니다.
ClassName.methodName();
  • 예를 들어, Math 클래스의 abs 메서드는 다음과 같이 호출합니다.
int result = Math.abs(-10); // 결과: 10
  • 아래 예시와 같이 printMessage 메서드는 Utility 클래스에 속한 static 메서드로, 객체를 생성하지 않고도 호출할 수 있습니다.
public class Utility {
    public static void printMessage(String message) {
        System.out.println(message);
    }
}

public class Main {
    public static void main(String[] args) {
        Utility.printMessage("Hello, World!"); // 객체 생성 없이 호출
    }
}

인스턴스 메서드

💡인스턴스 메서드란?

  • 인스턴스 메서드는 static 키워드 없이 선언된 메서드로, 특정 객체(인스턴스)에 속합니다. 따라서 이러한 메서드를 호출하려면 해당 클래스의 객체를 먼저 생성해야 합니다. [각주:2]
Instance methods are the methods that require an object to work. We need to create an object of the class where the method is written, then only we can access the instance method.

인스턴스 메서드 사용 방법

  • 인스턴스 메서드는 객체를 생성한 후, 해당 객체를 통해 호출합니다.
ClassName obj = new ClassName();
obj.methodName();
  • 예를 들어, Calculator 클래스의 add 메서드는 다음과 같이 호출합니다.
Calculator calc = new Calculator();
int result = calc.add(5, 3); // 결과: 8
  • 아래 예시와 같이 greet 메서드는 Greeter 클래스의 인스턴스 메서드로, 객체를 생성한 후에 호출됩니다.
public class Greeter {
    public void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }
}

public class Main {
    public static void main(String[] args) {
        Greeter greeter = new Greeter(); // 객체 생성
        greeter.greet("World"); // 인스턴스 메서드 호출
    }
}

static VS 인스턴스 메서드

항목 static 메서드 인스턴스 메서드
소속 클래스 객체(인스턴스)
호출 방법 클래스명.메서드명() 객체참조변수.메서드명()
인스턴스 멤버 접근 불가능 (객체 생성 후 참조를 통해 접근 가능) 가능
this 키워드 사용 불가능 가능
메모리 위치 메서드 영역 메서드 영역
사용 목적 유틸리티 메서드, 공통 기능 제공 등 객체의 상태를 변경하거나 객체에 의존하는 동작 수행
  • 인스턴스 메서드 : static이 붙지 않은 멤버 메서드
  • 클래스 메서드 : static이 붙은 멤버 메서드

출처

https://www.inflearn.com/course/김영한의-실전-자바-기본편?srsltid=AfmBOorsKsHF2yYZ6xTn8xNWTCfuLaexrv2zJD7mTTK9PvoOIZBZS9Eo

 

김영한의 실전 자바 - 기본편 강의 | 김영한 - 인프런

김영한 | , 국내 개발 분야 누적 수강생 1위, 제대로 만든 김영한의 실전 자바[사진][임베딩 영상]단순히 자바 문법을 안다? 이걸로는 안됩니다!전 우아한형제들 기술이사, 누적 수강생 40만 명 돌

www.inflearn.com


주석 출처