자바 수정자
수정자
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
.
예시
static
및 public
메소드 의 차이점을 보여주는 예 :
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
}
}