자바 튜토리얼

자바 홈 자바 소개 자바 시작하기 자바 구문 자바 주석 자바 변수 자바 데이터 유형 자바 타입 캐스팅 자바 연산자 자바 문자열 자바 수학 자바 부울 자바 If...Else 자바 스위치 자바 while 루프 자바 For 루프 자바 중단/계속 자바 배열

자바 메소드

자바 메소드 자바 메소드 매개변수 자바 메소드 오버로딩 자바 범위 자바 재귀

자바 클래스

자바 OOP 자바 클래스/객체 자바 클래스 속성 자바 클래스 메소드 자바 생성자 자바 수정자 자바 캡슐화 자바 패키지 / API 자바 상속 자바 다형성 자바 내부 클래스 자바 추상화 자바 인터페이스 자바 열거형 자바 사용자 입력 자바 날짜 자바 배열 목록 자바 링크드리스트 자바 해시맵 자바 해시셋 자바 반복자 자바 래퍼 클래스 자바 예외 자바 정규식 자바 스레드 자바 람다

자바 파일 처리

자바 파일 자바 파일 생성/쓰기 자바 읽기 파일 자바 삭제 파일

자바 방법

두 개의 숫자 더하기

자바 참조

자바 키워드 자바 문자열 메소드 자바 수학 메소드

자바 예제

자바 예제 자바 컴파일러 자바 연습 자바 퀴즈 자바 인증서


자바 수정자


수정자

public지금쯤이면 거의 모든 예에 나타나는 키워드 에 매우 익숙할 것입니다.

public class Main

public키워드는 액세스 수정자 이며, 이는 클래스, 속성, 메서드 및 생성자에 대한 액세스 수준을 설정하는 데 사용됨을 의미합니다.

수정자를 두 그룹으로 나눕니다.

  • 액세스 수정자 - 액세스 수준을 제어합니다.
  • Non-Access Modifiers - 액세스 수준을 제어하지 않지만 다른 기능을 제공합니다.

액세스 수정자

클래스 의 경우 public또는 기본값 을 사용할 수 있습니다 .

Modifier Description Try it
public The class is accessible by any other class
default The class is only accessible by classes in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter

속성, 메서드 및 생성자 의 경우 다음 중 하나를 사용할 수 있습니다.

Modifier Description Try it
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter
protected The code is accessible in the same package and subclasses. You will learn more about subclasses and superclasses in the Inheritance chapter

비접근 수정자

클래스 의 경우 final또는 abstract다음 중 하나를 사용할 수 있습니다 .

Modifier Description Try it
final The class cannot be inherited by other classes (You will learn more about inheritance in the Inheritance chapter)
abstract The class cannot be used to create objects (To access an abstract class, it must be inherited from another class. You will learn more about inheritance and abstraction in the Inheritance and Abstraction chapters)

속성 및 메서드 의 경우 다음 중 하나를 사용할 수 있습니다.

Modifier Description
final Attributes and methods cannot be overridden/modified
static Attributes and methods belongs to the class, rather than an object
abstract Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from). You will learn more about inheritance and abstraction in the Inheritance and Abstraction chapters
transient Attributes and methods are skipped when serializing the object containing them
synchronized Methods can only be accessed by one thread at a time
volatile The value of an attribute is not cached thread-locally, and is always read from the "main memory"


결정적인

기존 속성 값을 재정의하는 기능을 원하지 않으면 속성을 final다음 과 같이 선언합니다.

예시

public class Main {
  final int x = 10;
  final double PI = 3.14;

  public static void main(String[] args) {
    Main myObj = new Main();
    myObj.x = 50; // will generate an error: cannot assign a value to a final variable
    myObj.PI = 25; // will generate an error: cannot assign a value to a final variable
    System.out.println(myObj.x);
  }
}


공전

메소드는 다음 static과 달리 클래스의 객체를 생성하지 않고 액세스할 수 있음을 의미합니다 public.

예시

staticpublic메소드 의 차이점을 보여주는 예 :

public class Main {
  // Static method
  static void myStaticMethod() {
    System.out.println("Static methods can be called without creating objects");
  }

  // Public method
  public void myPublicMethod() {
    System.out.println("Public methods must be called by creating objects");
  }

  // Main method
  public static void main(String[ ] args) {
    myStaticMethod(); // Call the static method
    // myPublicMethod(); This would output an error

    Main myObj = new Main(); // Create an object of Main
    myObj.myPublicMethod(); // Call the public method
  }
}


추상적 인

메서드는 클래스에 속하며 본문이 없습니다 abstract. abstract본문은 하위 클래스에서 제공합니다.

예시

// Code from filename: Main.java
// abstract class
abstract class Main {   public String fname = "John";   public int age = 24;   public abstract void study(); // abstract method } // Subclass (inherit from Main) class Student extends Main {   public int graduationYear = 2018;   public void study() { // the body of the abstract method is provided here     System.out.println("Studying all day long");   } } // End code from filename: Main.java // Code from filename: Second.java class Second {   public static void main(String[] args) {     // create an object of the Student class (which inherits attributes and methods from Main)     Student myObj = new Student();     System.out.println("Name: " + myObj.fname);     System.out.println("Age: " + myObj.age);     System.out.println("Graduation Year: " + myObj.graduationYear);     myObj.study(); // call abstract method
  } }