LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java : Meta variables ? (https://www.linuxquestions.org/questions/programming-9/java-meta-variables-170418/)

pycoucou 04-15-2004 06:06 AM

Java : Meta variables ?
 
Well, I don't know if it's possible... It's from memories about C/C++.

Is it possible to define global variable, like DEBUG, DISPLAY, which could tune displayed information... Typically, while debugging, I want to see every thing but later on, no time for that.

I don't know how to do that, except of course, by adding an extra variable to all my methods but it would be neither tractable nor handy.

Thanks. Cheers,
Pierre-Yves

Mega Man X 04-15-2004 09:54 AM

As far as I know, java does not work with Global Variables. You could defined a static variable at your highest class and all other class would inheritance that variable, but that is it :)

pycoucou 04-15-2004 09:57 AM

Thanks Megaman... My programs won't have the possibility to change their log level...

:)

Mega Man X 04-15-2004 09:59 AM

Cool, my pleasure. I could be wrong though :) That has been a long since I last touched Java. I might start at it again, it was so cool :)

pycoucou 04-15-2004 10:08 AM

I always trust someone who says 'There's no point to try, get some rest'... ;-)

german 04-15-2004 08:14 PM

Java still has no concept of true global variables, but as far as I'm concerned it shouldn't anyway cuz it's pure OO by design (as a rule), and thus every variable should have a scope. You might serve better to do something like setting what System.out is with System.setOut(java.io.PrintWriter) at convenient times... like:

if(System.getProperty("debug") == null) {
System.setOut(new NullPrintWriter());
}

...

class NullPrintWriter extends PrintWriter {
(some code to do nothing when you write to it)
}

This is all from memory but I'm sure it's not that far off... it might be an outputstream instead of a printwriter.

HTH

B.

moeminhtun 04-15-2004 10:12 PM

One way you can achieve is that, create a new class and define a static variables inside.
For example,

public class LogLevel {
public static final int DISPLAY = 0;
public static final int DEBUG = 1;
public static final int RELEASE = 2;
}

and you can refer to these variables from anywhere in your program like,

LogLevel.DEBUG
LogLevel.RELEASE

so it's the same as the global variables in C++ except that you need to add the class name in front.


But the more efficient way is that, you can use the logging facilities. Jdk1.4 and onwards have the build-in logging features with different levels.
And there is more powerful, and feature-rich 3rd party tool available for logging from http://www.apache.org , which is called log4j under the jakarta project which is very popular and a lot of ppl using it.

pycoucou 04-16-2004 03:46 AM

Thanks very much... I'll have a look... ;-)


All times are GMT -5. The time now is 06:40 AM.