초기화 블록 (Initialization Block)


- static 변수를 초기화 하기 위한 static 초기화 블록은 프로그램 시작시 단 한 번 실행

- 인스턴스 초기화 블록은 객체를 생성할 때마다 실행 (일반적으로 생성자를 사용하지만 생성자보다 먼저 실행)


public class dailyCode {

static{

//once run

System.out.println("run when start program");

}

{

//whenever create instance

System.out.println("run when create instance");

}


public static void main(String[] args){

System.out.println("run main");

dailyCode test1 = new dailyCode();

dailyCode test2 = new dailyCode();

}


'프로그래밍 > JAVA' 카테고리의 다른 글

arraycopy Example  (0) 2016.11.19
scheduleAtFixedRate vs. scheduleWithFixedDelay  (0) 2016.11.18
Callback using interface  (0) 2016.11.17
Singleton Design Pattern  (0) 2016.11.16
HashSet 예제  (0) 2016.11.15
ArrayList 예제  (0) 2016.11.14
생성자 예제 (Constructor Example)  (0) 2016.11.13

HashSet 예제


- Set 계열의 특징 : 중복 저장하지 않으며 저장 순서 없음


import java.util.HashSet;;


public class dailyCode {

public static void main(String[] args){

HashSet set = new HashSet();

HashSet setCopy = new HashSet();

set.add("one");//autoboxing

set.add("one");

set.add("two");

set.add("one");

set.add(new String("three"));//Wrapper class

set.add(1);

set.add(100.5);

System.out.println("set : " + set.toString());

setCopy.addAll(set);

System.out.println("setCopy : " + setCopy.toString());

set.remove("one");

System.out.println("set : " + set.toString());

setCopy.clear();

System.out.println("setCopy : " + setCopy.toString());

}


'프로그래밍 > JAVA' 카테고리의 다른 글

arraycopy Example  (0) 2016.11.19
scheduleAtFixedRate vs. scheduleWithFixedDelay  (0) 2016.11.18
Callback using interface  (0) 2016.11.17
Singleton Design Pattern  (0) 2016.11.16
초기화 블록 (Initialization Block)  (0) 2016.11.15
ArrayList 예제  (0) 2016.11.14
생성자 예제 (Constructor Example)  (0) 2016.11.13

ArrayList 예제


- List 계열 특징 : 중복 저장 가능하며 저장 순서 있음


import java.util.ArrayList;


public class dailyCode {

public static void main(String []args){

ArrayList list = new ArrayList<>();

String[] add = new String[5];

int i = 0;

for(String str : add){

str = "add" + i++;

list.add(str);

}

for(int j=0; j<list.size(); j++){

System.out.println(list.get(j));

}

list.set(0, "set0");

list.remove(3);

list.remove("add4");

System.out.println("----");

for(int j=0; j<list.size(); j++){

System.out.println(list.get(j));

}

}


'프로그래밍 > JAVA' 카테고리의 다른 글

arraycopy Example  (0) 2016.11.19
scheduleAtFixedRate vs. scheduleWithFixedDelay  (0) 2016.11.18
Callback using interface  (0) 2016.11.17
Singleton Design Pattern  (0) 2016.11.16
초기화 블록 (Initialization Block)  (0) 2016.11.15
HashSet 예제  (0) 2016.11.15
생성자 예제 (Constructor Example)  (0) 2016.11.13

+ Recent posts