2

I know I'm throwing a pretty stupid question, but I promise I did try to search around the settings and around google and the webs and no luck, so I guess I should just ask.

I saw in quite a few open-source projects the following line:

if(DEBUG){
    //  do some logging
}

I perfectly understand what the line is doing, I just can't seem to repeat it. There's no declaration of boolean DEBUG; anywhere in the code, and I couldn't figure out a place on the project properties to define a system wide variable to be replaced on the compiling (into byte code). And as far as I know, if it's a reference to a class constant it was supposed to be Log.DEBUG or something similar.

Can anyone solve this mystery for me? thanks.

5
  • Did you look in your imports to see if it was there? It could be a static import, which would explain why it is not declared in the source code. Commented Oct 4, 2012 at 13:57
  • I'm guessing they're all inheriting from some class that does have the constant defined, probably initializing it from some settings file... Commented Oct 4, 2012 at 13:57
  • 2
    If you're using Eclipse: Put the cursor on the word DEBUG and press F3. It should jump to the declaration of DEBUG. Commented Oct 4, 2012 at 13:59
  • @Jesper - Taught me something new. I normally use ctrl+click which does the same thing, but always good to learn another way =) Commented Oct 4, 2012 at 14:02
  • It was mostly on GitHub or google.code, but it's for Android so I'm 95% sure it was on Eclipse. I'm searching for some of those projects so I can post here. Commented Oct 4, 2012 at 14:16

2 Answers 2

3

You really should look at the import section of the class.

It is probably a static import, like this :


A.java

public class A {
    public static boolean DEBUG = false;
}

B.java

import static A.DEBUG;

public class B {
  public void myMethod() {
    if (DEBUG) {
      // do something
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know we're not supposed to do "thank you" comments. But awesome! That's the way to keep in a single location the debug flag value and use around the application. Then, with one line change you can compile a production version. Thanks.
0

You are in front of a public static field, look in import list and pick the original class or, use the inline help from Eclipse as said in comments

4 Comments

I tested creating public static final boolean DEBUG in class1.java and in class2.java importing class1.java and use DEBUG, but unless I write class1.DEBUG it won't recognise. It's probably something on the project configuration. I know on the TexasInstrument IDE for their microcontrolers there's a section you can declare constants, but that's C for embed uC
@Budius You are right, then verify if your class extends anotehr class (or an abstract)
that's something I saw on GitHub or google.code and always bugged me. I remembered now, asked some ppl around work, no one knows. But at least now is registered and next time I see one I'll check out the project, dig into it and post the answer back in here.
@Budius I saw also it in an old version of an OODB src. I'm quite certain it is bound to inheritence. As I use log4j, I've forgotten this way of codding - As you, if I found something about this, I'll post here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.