728x90
★ 내부 클래스
class A { :: B의 외부 클래스
class B { } :: 내부 클래스
=> B클래스가 A 클래스 에서만 이용될때
=> 외부클래스인 A 의 맴버에 객체 생성 없이 접근 가능
}
class TestClass {
private int outerIv = 0;
private static int outerCv = 0;
class InstancsInner {
int iv = 111;
//static int cb = 100; // static 클래스만 static 맴버 정의 가능
final static int CONST = 100; // 상수는 허용
StaticInner obj2 = new StaticInner(); //인스턴스 맴버 => static 맴버 접근가능
}
static class StaticInner{
int iv = 222;
static int cv = 200; // static 클래스만 static 맴버 정의 가능
//InstancsInner obj1 = new InstancsInner();//static 맴버 =>인스턴스맴버 접근불가
}
void myMethod() {
final int ib = 1; // 상수( 불변 )만 사용 가능
final int LV = 1; // jdk 1.8 부터 [ 값이 바뀌지만 않으면 ] final 생략 가능
class LocalInner{ // 지역 내부 클래스
int iv = 300;
//static int cv = 300; // static 클래스만 static 맴버 정의 가능
final static int CONST = 300;// 상수는 허용
int test1 = outerIv; //test class 의 iv
int test2 = outerCv; //
int ib2 = ib; // myMethod 의 맴버에 객체 생성 없이 접근 가능
int LV2 = LV;
}
}
}
public class hello {
public static void main(String[] args) {
//내부 인스턴스 class 접근
TestClass tj = new TestClass();
TestClass.InstancsInner my = tj.new InstancsInner();
System.out.println(my.iv);
//내부 static 클래스 접근
TestClass.StaticInner sta = new TestClass.StaticInner();
System.out.println(sta.iv);
}
}
★ 익명 클래스
=> 이름이 없는 일회용 클래스, 정의와 생성을 동시에 한다.


★ 래퍼클래스
=> 8개의 기본형을 객체로 다뤄야 할때 사용하는 클래스
Boolean , Character , Byte , Short , Integer , Long , Float , Double
=> Integer.MAX_VALUE , Integer.MIN_VALUE
=> Integer.SIZE , Integer.BYTES , Integer.TYPE
=> Integer i = new Integer(100);
★ T 타입 객체의 래퍼클래스 Optional<T>
=> null을 직접 다루는 것은 위험하다.
=> null 체크시 if 문 필수 > 코드 지저분
Optional<String> optVal = Optional.of("abc");
Optional<String> optVal = Optional.ofNullable(null);
String str1 = optVal.get() :: 저장된 값을 반환 , null 이면 예외 발생
String str1 = optVal.orElse("") :: 저장된 값이 null인경우 "" 반환
String str2 = optVal.orElseer(String::new) :: 람다식 사용가능 () -> new String()
String str2 = optVal.orElseThrow(NullPointerException::new) :: null이면 예외 발생
if( Optional.ofNullable(str).ifPresent() ) :: null이면 false
★ 기본형 값을 감싸는 래퍼 클래스
OptionalInt , OptionalLong , OptionalDouble
OptionalInt opt = OptionalInt.of(0);
OptionalInt opt2 = OptionalInt.empty();
System.out.printf(opt.isPresent) :: true
System.out.printf(opt2.isPresent) :: false
System.out.printf(opt.equals(opt2)) :: false
Integer a = opt.getAsInt();
Long a = opt.getAsLong();
Double a = opt.getAsDouble();
★ Vector 클래스 :: 가변배열 가능
Object 배열을 가지고 있어서 모든종류의 객체를 저장 할 수있다.
728x90