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 |
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 :)
|
Thanks Megaman... My programs won't have the possibility to change their log level...
:) |
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 :)
|
I always trust someone who says 'There's no point to try, get some rest'... ;-)
|
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. |
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. |
Thanks very much... I'll have a look... ;-)
|
All times are GMT -5. The time now is 06:40 AM. |