자바에서 조건부 컴파일 흉내내기

자바에서 C++의 조건부 컴파일 흉내를 내려면 아래와 같이 할 수 있다.

public class test {
 static final boolean DEBUG = false;
 
 public static void main(String[] args) {
  System.out.println("######## 1");
  if (DEBUG) {
   System.out.println("######## 2");
  }
   
  System.out.println("######## 3");
 }
}

 

자바에서 위의 코드를 컴파일하고 나서 생성되는 .class 파일을 디컴파일 해서 보면 아래와 같다.

 

public class test
{
    public test()
    {
    }

    public static void main(String args[])
    {
        System.out.println("######## 1");
        System.out.println("######## 3");
    }

    static final boolean DEBUG = false;
}

 

DEBUG 값이 true 일 경우에는 아래아 같다.

public class test
{
    public test()
    {
    }

    public static void main(String args[])
    {
        System.out.println("######## 1");
        System.out.println("######## 2");
        System.out.println("######## 3");
    }

    static final boolean DEBUG = false;
}

 

확장하여 아래와 같이 사용할 수도 있다.

public class test { 
 public static void main(String[] args) {
  System.out.println("######## 1");
  if (test2.DEBUG) {
   System.out.println("######## 2");
  }
   
  System.out.println("######## 3");
 }
}

public class tes2 {
  static final boolean DEBUG = false;
}

Related Posts

답글 남기기

이메일 주소는 공개되지 않습니다.