Open hyunjungkimm opened 1 week ago
값을 바꿀 수 없다.
public class FinalVariable {
int instanceVariable;
final int instanceVariable2 = 2;
//final int instanceVariable2; -- 에러, 인스턴스 변수, 클래스 변수 : 생성과 동시에 초기화
// 매개 변수나 지역 변수를 final로 선언하는 경우에는 반드시 선언할 때 초기화할 필요는 없다.
public void method(final int parameter) {
final int localVariable;
localVariable = 2;
// 매개 변수 : 값이 넘어오기 전에 이미 값을 정해 놨기 때문에, 다시 할당 x
// parameter = 4;
}
}
package chapter13;
import chapter12.MemberDTO;
public class FinalReferenceType {
final MemberDTO dto = new MemberDTO();
public static void main(String[] args) {
FinalReferenceType finalReferenceType = new FinalReferenceType();
finalReferenceType.checkDTO();
}
public void checkDTO() {
System.out.println(dto);
//기본자료형과 마찬가지로 참조 자료형도 두 번 이상 할당하거나 새로 생성자를 사용하여 초기화할 수 없음
//dto = new MemberDTO();
dto.name = "Sangmin";
System.out.println(dto);
}
}
try {
//예외가 발생 가능한 문장
} catch(예외1 e1) {
//예외1이 발생했을 때 처리 문장
} catch(예외2 e2) {
//예외2가 발생했을 때 처리 문장
} finally {
// try나 catch가 어떻게 수행되었든 간에 수행되는 문장
}
String(byte[] bytes)
: 현재 사용중인 플랫폼의 캐릭터 셋을 사용하여 제공된 byte 배열을 디코딩한 String 객체를 생성함String(byte[] bytes, String charsetName)
: 지정한 이름을 갖는 캐릭터 셋을 사용하여 지정한 byte 배열을 디코딩한 String 객체를 생성함
import java.util.EventListener;
public class NestedValueReference { public int publicInt = 0; protected int protectedInt = 1; int justInt = 2; private int privateInt = 3; static int staticInt = 4;
//staticNested 클래스 - static 변수만 참조 가능
static class StaticNested {
public void setValue() {
//static으로 선언되어 있어서 부모 클래스에 static하지 않은 변수 참조 불가능
staticInt = 14;
}
}
//내부 클래스- 감싸고 있는 클래스의 어떤 변수라도 참조 가능
class Inner {
public void setValue() {
publicInt = 20;
protectedInt = 21;
justInt = 22;
privateInt = 23;
staticInt = 24;
}
}
//익명 클래스 - 감싸고 있는 클래스의 어떤 변수라도 참조 가능
public void setValue() {
EventListener listener = new EventListener() {
public void onClick() {
publicInt = 30;
protectedInt = 31;
justInt = 32;
privateInt = 33;
staticInt = 34;
}
};
}
}
public class ReferenceAtNested { static class StaticNested { private int staticNested = 99; }
class Inner {
private int inner = 100;
}
// 클래스의 객체 생성한 후 그 값을 참조하는 것은 가능함
//private이어도 접근 가능
public void setValue(int value) {
ReferenceAtNested.StaticNested nested = new ReferenceAtNested.StaticNested();
nested.staticNested = value;
Inner inner = new Inner();
inner.inner = value;
}
}
## 1. Static nested class
- 클래스 선언할 때 static으로 선언함
- 한 곳에서만 사용되는 클래스를 논리적으로 묶어서 처리할 필요가 있을 때
## 2. inner(내부) class
- 클래스 선언할 때 static 없이 선언함
- 캡슐화(하나의 클래스에서 어떤 공통적인 작업을 수행하는 클래스가 필요한데 다른 클래스에서는 그 클래스가 전혀 필요가 없을 때 이럴때 내부 클래스를 만들어 사용한다.)가 필요할때, 내부 구현을 감추고 싶을때
- 다른 곳에서 재사용할 일이 없을때 만들어야 함
### 2-1. Local inner class
### 2-2. Anonymous inner(내부 익명) class
- 이름이 없는 클래스
- 다른 클래스나 메소드에서는 참조할 수 없다.
@interface
@Target
@Retention
@Documented
@Inherited
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface UseAnnotation { int number(); String text() default "first annotation"; }
public class UserAnnotationSample { @UseAnnotation(number = 0) public static void main(String[] args) { UserAnnotationSample sample = new UserAnnotationSample(); }
@UseAnnotation(number = 1)
public void annotationSample1(){
}
@UseAnnotation(number = 2, text = "second")
public void annotationSample2(){
}
@UseAnnotation(number = 3, text = "third")
public void annotationSample3(){
}
}
import java.lang.reflect.Method;
public class UserAnnotationCheck { public static void main(String[] args) { UserAnnotationCheck sample = new UserAnnotationCheck(); sample.checkAnnotations(UserAnnotationSample.class); }
private void checkAnnotations(Class useClass) {
Method[] methods = useClass.getDeclaredMethods();
for(Method method : methods) {
UseAnnotation annotation = method.getAnnotation(UseAnnotation.class);
if(annotation != null) {
int number = annotation.number();
String text = annotation.text();
System.out.printf("""
%s () : number = %d, text = %s
""", method.getName(), number, text);
}else {
System.out.printf("""
%s () : annotation is null
""", method.getName());
}
}
}
} /* main () : number = 0, text = first annotation annotationSample1 () : number = 1, text = first annotation annotationSample2 () : number = 2, text = second annotationSample3 () : number = 3, text = third
클래스
캡슐화(Encapsulation)
객체(Object)
변수의 종료
Overriding, Overloading의 차이점
Overriding
Overloading
상속
생성자
메소드
변수
형변환
다형성(Polymorphism)
java.lang.Object 클래스
주요 메소드
객체를 처리하기 위한 메소드