Java-11강

2010년 1월 7일 11강


Class(static) Variable

Are shared among all instances of a class
static – 공유변수

Class(static) Methods

주소없이 접근하기 위해
new 없이 접근하기 위해 static을 쓴다.

static Initializers 스태틱 초기화 블럭

  • static block code executes only once, when the class is loaded
  • static block is usually used to initialize static attributes
ex)
private final int YEAR; – final이 붙은 멤버변수는 초기화는 생성자에서..
private static final int DAY; – static final 의 초기화는 static 초기화 블록에서 초기화..
변수의 초기화는 생성자나 초기화블록을 통해서..
클래스에 static을 사용하려면 내부클래스여야만 사용가능

The final Keywords

class 앞에 쓸 수 있는 (access) modifier : public, [default], abstract, final class Demo

variable 앞에 : public, private, protected, [default] int su;

method 앞에 : public, private, protected, [default], final, static, abstract, int method()
멤버, 스태틱, 지역 상수

Deprecation

//…

Inner Class

allow a class definition to be placed inside another class definition.
  • Nested Class
  • Member Class
  • Local Class
  • Anonymous Class
1.why?
2.생성방법
3.제한사항

Nested Class

/*
* static class(Nested Class)
* 1. why: class를 packaging 하기 위해
* 2. 생성방법: Outer.Inner in = new Outer.Inner();
* 즉 바깥클래스의 주소 필요하지 않다.
* 3. 제한사항: outer class의 멤버변수, 멤버메소드 접근 불가
*
*/
packaging..하기위한..
class com{
static class javasoft{
static class lib{
static class Test{}
}
}
static class jspsoft{
static class lib{}
}
}
com.javasoft.lib.Test t = new com.javasoft.lib.Test();
Member class
/*
* member class
* 1. why?: 다중상속가능
* 2. 생성방법: 바깥쪽 클래스의 주소가 필요하다.
* Outer.Inner in = out.new Inner();
* 3. 제한사항: static 변수나 static method를 가질 수 없다.
*/
static variable, method 를 가질 수 없다.
다중상속 가능.

Local Class

/*
* Local class
* 1. why?: scope를 가장 짧게하기 위해서, 메소드가 실행되는 동안만 가능
* 2. 생성방법: 자기가 속한 메소드를 호출해야만 생성가능
* 3. 제한사항: 1) 자기가 속한 메소드의 순서를 지켜야한다.
* 2) access modifier 사용할 수 없다.
* 3) static 사용할 수 없다
* 4) 바깥 클래스의 멤버변수, 스태틱변수 접근가능
* 자기가 속한 메소드, 지역변수 접근 불가능
* 자기가 속한 지역 상수만 접근가능
*
*/

Anonymous Class

local class의 제한사항과 똑같음

enum type

(실제 필드에서는 잘 안쓴다..)
class Weekend{
public static final int SUN = 0;
public static final int MON = 1;
}
interface Weekend{
int SUN = 0;
int MON = 1;
}
enum Weekend{
SUN, MON;
}
enum에는 data type을 입력하지 않음
// 상수들만 되어있기 때문에 new를 쓸 필요없다.
// 클래스가 아니므로
//Weekend w = new Weekend();
Java API

java.lang.Enum<E>

switch문에 사용한다.
values()를 이용해서 for each를 이용 배열로 뽑아낼 수 있다.
SCJP에서는 enum 알아야함
Comment are closed.