LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-15-2004, 07:06 AM   #1
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
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
 
Old 04-15-2004, 10:54 AM   #2
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
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
 
Old 04-15-2004, 10:57 AM   #3
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
Thanks Megaman... My programs won't have the possibility to change their log level...

 
Old 04-15-2004, 10:59 AM   #4
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
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
 
Old 04-15-2004, 11:08 AM   #5
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
I always trust someone who says 'There's no point to try, get some rest'... ;-)
 
Old 04-15-2004, 09:14 PM   #6
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
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.
 
Old 04-15-2004, 11:12 PM   #7
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
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.

Last edited by moeminhtun; 04-16-2004 at 03:59 AM.
 
Old 04-16-2004, 04:46 AM   #8
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
Thanks very much... I'll have a look... ;-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Xterm and meta characters Khabi Linux - Software 1 09-20-2005 05:31 PM
Setting environment variables in Java linuxprogrammer Linux - General 2 03-03-2005 06:09 PM
meta keys: and so forth ... wuck Linux - Software 2 10-17-2003 02:34 PM
environment variables with java lilrowdy18 Linux - Software 1 08-20-2003 06:43 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 12:07 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration