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
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