Constructor & Modifiers

이상해씨·2023년 4월 26일
0

JAVA

목록 보기
14/40

Constructor

  • 객체를 초기화하는데 사용되는 특별한 메소드
  • 클래스의 객체가 생성될 때 호출됨
  • 객체 속성의 초기값(initial values) 을 설정하는데 사용
// Create a Main class
public class Main {
  int x;  // Create a class attribute

  // Create a class constructor for the Main class
  public Main() {
    x = 5;  // Set the initial value for the class attribute x
  }

  public static void main(String[] args) {
    Main myObj = new Main(); // Create an object of class Main (This will call the constructor)
    System.out.println(myObj.x); // Print the value of x
  }
}

// Outputs 5

(W3school: https://www.w3schools.com/java/java_constructors.asp)

Modifiers

  • public과 유사.

    	```
    	public class Main
    	```

1) Access Modifiers : 접근 수준을 통제
2) Non Access Modifier : 접근 수준 통제 하지 않음, 다른 기능을 제공

1) Access Modifiers

1-1) class

  • public : 다른 클래스로부터 접근가능한 클래스
  • default : 같은 패키지 안의 클래스만 접근가능. modifier를 명시하지 않아도 됨.

1-2) attributes, methods, constructors

  • public: 모든 클래스가 접근할 수 있는 코드
  • private : 명시된 클래스안에서만 접근가능
  • default : 같은 패키지 안에서만 접근가능. modifier 명시하지 않아도 됨.
  • proteced : 같은 패키지와 서브클래스 안에서만 접근가능한 코드. (상속)

2) Non Access Modifiers

2-1) class

  • final : 다른 클래스들로부터 상속받지 않음
  • abstract : 객체를 생성하지 않음. abstract class에 접근하기 위해서는 다른 클래스로부터 상속 받아야 함. (상속, 추상화)

2-2) attributes, methods

  • final : 재정의, 수정(overriden/modified) 될 수 없는 메소드
  • static : 객체가 아닌 클래스에 속하는 메소드와 속성
  • abstract : 추상 클래스에서만 사용. 메소드에서만 사용. 메소드는 body가 없고, 서브 클래스가 있음.
  • transient : 속성과 메소드를 포함하는 객테가 직렬적일 때 해단 특성 및 메소드를 건너뛴다. (Attributes and methods are skipped when serializing the object containing them)
  • synchronized : 동기화된 메소드는 한번에 하나의 스레드에 의해서만 접근할 수 있다.
  • volatile : main memory에서 항상 읽음. 휘발성의 속성 값은 스레드 로컬로 캐시되지 않음 (The value of an attribute is not cached thread-locall)

3) 중요한 개념

1) 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);
  }
}

(w2school :https://www.w3schools.com/java/java_modifiers.asp)

2) 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
  }
}

(w2school :https://www.w3schools.com/java/java_modifiers.asp)

3) Abstract

  • abstract 는 abstract class에 속한다.
  • body는 subclass이기 때문에 body가 없다.
// 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
  }
}

(w2school :https://www.w3schools.com/java/java_modifiers.asp)


참고

profile
공부에는 끝이 없다

0개의 댓글