codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Accessing main variables  kresjer at 15:28 on Monday, September 12, 2005
 

I'm writing a card game, and I made a class for the current status of the game. A lot of different components need to access the gamestatus, and work with it. Now I don't want every component to have their own private object variables because then I'd have to change them all if the game status changes. So I want each object to access the main status so I just want each object to have some kind of reference to the main variable.

Put into a simple example, I'd like this to output "foobar"

public class Main {
static String hier;
public static void main(String[] args) {
text = "foo";
myString i = new myString(text);
System.out.println(i.get()); // output foo
hier = hier + "bar";
System.out.println(i.get()); // still outputs foo
}
}

class myString
{
public String s;
public myString(String st)
{
s = st;
}
public String get()
{
return s;
}
}

Thanks for your help!

  Re: Accessing main variables  javabits at 21:07 on Monday, September 12, 2005
 

Use what's called a singleton (It's one of the major Design Patterns). It would look something like this.

public class Main {
static String hier;
public static void main(String[] args) {
text = "foo";
myString i = myString.init(text);
System.out.println(i.get()); // output foo
hier = hier + "bar";
System.out.println(i.get()); // still outputs foo
}
}

public class myString
{
private String s;
private static myString myStringInstance;
private myString(String st)
{
s = st;
}

public static myString init(String st)
{
myStringInstance = new myString(st);
return myStringInstance;
}

public static myString getInstance()
{
return myStringInstance;
}

public static String get()
{
return s;
}
}

semper fi...

  Re: Accessing main variables  kresjer at 09:21 on Tuesday, September 13, 2005
 

I've read about those Singletons, but I don't see how they can help me out in the current situation. Could you please tell me some more about that?

Thanks, Ciro.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Why Use Method?
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help
•  Re: How to make an investment calculator
•  Re: line breaks in GUI
•  Re: Graph in Gui...
•  Graph in Gui...
•  Re: Counting zero values in a string
•  Re: Help!


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005