Newbie question about creating 'global variables'

A

Alan Campbell

I have just started to use Java (and OO) and have a question which
could be answered by using global variables, but I was wondering if
there is a proper way of doing it.

I have a class, which contains the main method. In there I want to
initialise a class that handles my debug logging, a class that handles
my process logging , a class that handles my database access and a
class that processes some data.

The debug logging class has a method which switches debig on/off and a
method write a message out to err. the process logging class has an
overloaded method to write out a message to out, with an option of
displaying the time. The database class opens and closes the database
connection. The processing class just reads through a table and
outputs some of the contents.

I instantiate these classes in main. I want to be able to call the
debug class from within the database and processing class, but need to
know if debug was set on origionally and also use the database
connection that has been created by the database class in the
processing class.

Is the correct way :-

1) To set the variable in the debug class to static then create a new
instance of the debug class in each of the classes that needs to do
debug logging. That way the debug on/off flag is 'global'.

2) Pass a reference to the debug object created in main to all of the
classes that are called from main (and to any sub classes etc).

3) Again set the variable to static and call the object in main like
ClassWhereMainIs.DebugObject.print("xx").

4) None of the above !

The same thing applies to the database connection created by the
database class.

Option 1 looks like the simplest, but appears to go against the
concepts of OO !
Option 2 looks like the correct way, but also looks like a lot of
hassle. Every class will need to have the debug object, process
logging object and database object passed to it.
Option 3 looks fairly simple, but what if I wanted to share the debug
class with some other applications ?

Any pointers would be appreciated.

Alan
 
M

Marc Rochkind

I have just started to use Java (and OO) and have a question which
could be answered by using global variables, but I was wondering if
there is a proper way of doing it.

I have a class, which contains the main method. In there I want to
initialise a class that handles my debug logging, a class that handles
my process logging , a class that handles my database access and a
class that processes some data.

[snip]

In most of what you're saying, where you talk about the "class" doing such-
and-such, it's better to say that some OBJECT does such-and-such. I think
if you change your description to use the word "object" where it belongs
the whole thing will be much clearer to you.

Go ahead and design the classes, but the main program then should create an
object (or more than one) for each class. The various things that are done
are then done by invoking methods (defined by the class) of the various
objects.

The various object variables can be local to the main function, or
conceivably they could be defined in the class that contains main.
(Generally, the more local the better.) To record the state of any object,
define a field in its class and provide get and set methods for it.

This isn't an exact answer to your questions, but I think it's better to
start out on the right path first and then see if those same questions
arise with the new approach. They may not.

--Marc
 
A

Andy Flowers

Use the singleton pattern.

i.e.

for your debug class

class DebugSupport
{
private static debugSupportInstance = new DebugSupport();

private DebugSupport() // make constructor private
{
}

public static DebugSupport getInstance() // get the single debug instance
{
return debugSupportInstance;
}

public void writeDebugMessage( String message )
{
// do something with the message
}
}

and to call it use something like

class .....
{
...
private void useDebuggingExample()
{
...
if( debug )
{
DebugSupport.getInstance().writeDebugMessage( "This is a
message!!" );
}
}
}
 
S

Sebastian Millies

You can use this style of employing a factory method, but without
the restriction to a singleton. The getInstance method may be
parameterized, e.g. by the client's name. The static variable
debugLevel is accessed only from within DebugSupport. And you
can provide an enumeration of suitable debug levels in another
class (e.g. DebugLevel). If you only have two levels (on and off),
you could replace the setDebugLevel() method with two switchOn/
switchOff methods and keep the debug levels private to DebugSupport.

You might be interested to look at how well-known logging frameworks
like Jakarta Commons Logging or Log4J implement this kind of thing
- after all, the source is available.

-- Sebastian

public class DebugSupport

private static DebugLevel debugLevel = DebugLevel.OFF;

public static DebugSupport getInstance(String name) {
return new DebugSupport(name);
}

public static boolean isOn() {
return debugLevel.equals(DebugLevel.ON);
}

public static void setDebugLevel(DebugLevel debugLevel) {
this.debugLevel = debugLevel;
}

private DebugSupport(String name) {
this.name = name;
}

public void writeDebugMessage( String message ) {
if( DebugSupport.isOn() ) {
// output name, message, time etc.
}
}

private final String name;
}



And the getInstance-method may eve
 
R

Roedy Green

I instantiate these classes in main. I want to be able to call the
debug class from within the database and processing class, but need to
know if debug was set on origionally and also use the database
connection that has been created by the database class in the
processing class.

the simplest way to handle that is to have in one of the classes a

static public final boolean DEBUG = true;

The catch is, you have to recompile the universe if it ever changes.
The good news is, none of your debug code will be in the release at
all. It will all be optimised totally out of existence.
The bad news is, you can't turn it back on in the customer site if
needed.


Just leave off the final, and you have a switch that can be changed at
run time. The debug code will not be optimised away.
 
D

Dorothy Rifai

It's unclear whether you are doing this for a class assignment, or just
trying to create a debugging tool as a means of learning Java and/or a
tool for your future programming. Why don't you look at Log4J, a logging
system created by Apache (www.apache.org). It has been in use for
several years, is well written, and thoroughly debugged. It is open
source code, so you can look at how experienced programmers handled
debugging logs.

I think studying and implementing the Log4J code in a simple program you
wrote, maybe a "stub" program if you know what that is, would teach you
more than trying to re-invent the wheel.

You want to stay away from global variables. They are a programming
style which has a lot of drawbacks as well as advantages. If you feel
you have to use them in a program, you would create a text file which
contains the variables and their values (i.e. CreateLogFile=yes), and
then read them as static varibles. Don't hard-code values in your
programming code, and don't change the values of these variables in your
code.

Good luck,

Dorothy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top