Newbie question about creating 'global variables'

Discussion in 'Java' started by Alan Campbell, Jul 11, 2003.

  1. 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
     
    Alan Campbell, Jul 11, 2003
    #1
    1. Advertising

  2. On 11 Jul 2003 08:01:39 -0700, Alan Campbell <>
    wrote:

    > 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
     
    Marc Rochkind, Jul 11, 2003
    #2
    1. Advertising

  3. Alan Campbell

    Andy Flowers Guest

    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!!" );
    }
    }
    }
    "Alan Campbell" <> wrote in message
    news:...
    > 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
     
    Andy Flowers, Jul 11, 2003
    #3
  4. 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
    "Andy Flowers" <> schrieb im Newsbeitrag
    news:zBAPa.7227$...
    > Use the singleton pattern.
    > [code snipped]
     
    Sebastian Millies, Jul 11, 2003
    #4
  5. Alan Campbell

    Roedy Green Guest

    On 11 Jul 2003 08:01:39 -0700, (Alan
    Campbell) wrote or quoted :

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

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 11, 2003
    #5
  6. 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

    Alan Campbell wrote:
    > 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
     
    Dorothy Rifai, Jul 15, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mortal Wombat
    Replies:
    3
    Views:
    6,863
    Mortal Wombat
    Aug 7, 2003
  2. Wayne
    Replies:
    2
    Views:
    500
    Wayne
    Nov 11, 2003
  3. =?Utf-8?B?QW5kcmVhcyBIb2ZtYW5u?=

    Newbie question: migrating "global variables" from ASP to ASP.NET

    =?Utf-8?B?QW5kcmVhcyBIb2ZtYW5u?=, Apr 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    712
    =?Utf-8?B?QW5kcmVhcyBIb2ZtYW5u?=
    Apr 18, 2005
  4. jubelbrus
    Replies:
    5
    Views:
    644
    JohnQ
    Jul 20, 2007
  5. David

    Global Variables Newbie Question

    David, Oct 26, 2004, in forum: Javascript
    Replies:
    3
    Views:
    121
    Martin Walke
    Oct 28, 2004
Loading...

Share This Page