Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need global object/varible, but how? 1

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!

I want to have a class that holds some information like a path and some environment variables (unix). Problem is that I only want to read the environment variables and the path (from a text file) only once when the class is instantiated (or by calling a method once) and I want to have access to these values from every other class I use for the application I am writing.

Can this be done with static... hm... what... class, method, varibale?

Cheers

frag

patrick.metz@epost.de
 
There are many ways of doing this ... two are -

- look at the java.util.Properties class, which basically loads a file on the HD, and then you can access the values.

- Or create a trivial class that acts as a wrapper for your variables, and have some static methods or public variables that you can access - ie

String var = MyStaticClass.value1;
or
String var = MyStaticClass.getValue1();

and the class ...

public class MyStaticClass {
public static String value1 = "whatever";

// maybe have an accessor ...

public static String getValue1() {
return value1;
}
}
 
Ok thank you. Your example is what I was looking for. [2thumbsup] But if there is an accessor the variable itself is normally privat isn't it? ;-)

patrick.metz@epost.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top