Search code examples
javaconditional-compilation

Implement #ifdef in Java


I have an algorithm written in C and it has code which can be handled by C preprocessor but as there is no preprocessor in java i don't know how to write java code to handle such thing. The C code is

#ifdef Tile_size_utility
#define print_error 0
else
#define print_error 1
#endif

How can i implement this in Java?


Solution

  •  Class ErrorVerbose { 
              private static boolean enabled = False;
    
              public static setEnable(boolean enable) 
                    enabled = enable;
              }
    
              public static perror(String msg) {
    
                   if (enabled) {
                   /* Print */ 
                   }
    
              }
       }
    
    
     class YourClass {
    
               public YourClass(....,boolean status) {
    
                    ErrorVerbose.SetEnable(status)
                        .
                        . 
               }
    
       }
    

    Well, yes, either you can make is a non static class so that other classes can enable / disable verbose. Advantage of making a ErrorVerbose class is that you add more info like time, date, function name (which called), etc which makes it more informative I just gave a skeleton.