Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2007, 03:46 PM   PM User | #1
kehers
Regular Coder

 
Join Date: Mar 2006
Location: Nigeria
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
kehers is an unknown quantity at this point
Making Variables/Objects global across classes

Im writing a MIDlet app that contains some 5 classes. There are some variables and objects (date and filename for example) that is unique across the classes and to make this variables/objects assessible to the each of the class, I simply pass it as a parameter to the constructor. This however looks quack and does not support flexibility as I have to touch the constructors of each of the classes and their different instantiation points anytime I find out there is need to add another 'global' variable/object.
Is there anyway I can make the variables/objects 'Globally' accessible to all the classes without having to pass them as parameters?
kehers is offline   Reply With Quote
Old 12-10-2007, 04:10 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
If they are stored in one class, why not use get/set methods to access/manipulate the variables as needed?
brad211987 is offline   Reply With Quote
Old 12-14-2007, 09:26 AM   PM User | #3
kehers
Regular Coder

 
Join Date: Mar 2006
Location: Nigeria
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
kehers is an unknown quantity at this point
Quote:
If they are stored in one class, ...
The app has 5 different classes
kehers is offline   Reply With Quote
Old 12-14-2007, 01:59 PM   PM User | #4
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
You can make the variables static class variables and access them with static get/set methods. Using static variables and methods will keep the variables unique across instances of that class.

For example:

PHP Code:
public class MyClass
{
    private static 
int myVariable 5;

    public static 
void setMyVariable(int value)
    {
        
myVariable value;
    }

    public static 
int getMyVariable()
    {
        return 
myVariable;
    }

You would access this from your other class by:

PHP Code:
MyClass.setMyVariable(10);

int x MyClass.getMyVariable(); 
brad211987 is offline   Reply With Quote
Old 12-14-2007, 02:50 PM   PM User | #5
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
The first thing I would do is review you design of your code, having that many classes being dependent on each other may not be good style.

Alternately, you could do as Brad mentioned and make some of your classes inner-classes of another so that they have access to needed variables.
As an example, I've provided skeleton code from one of my midlets.
Code:
public class CameraMIDlet extends MIDlet
        implements CommandListener, Runnable {

private Display mDisplay;   //Main Midlet display
...more midlet code...

class PicCountCanvas extends Canvas
            implements CommandListener {

} //PicCountCanvas Class

class RecordAudio extends Thread {
        public void run(){
        ...
        }
}//RecordAudio Class

}//CameraMIDlet Class
All of these classes would then have access to the mDisplay variable declared by the CameraMIDlet Class.
Gox is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:41 AM.


Advertisement
Log in to turn off these ads.