pulic static varibles alternatives?

Hi,How do I access something that almost the whole application need to access (for ex a db connection)? Do I make it public and static? Or are there any better way?thanKS
[191 byte] By [spookyya] at [2007-9-28 8:45:42]
# 1

try using applicating scope variables.

In UR servlet:

ServletContext application = getServletConfig().getServletContext();

String connection = "URL";

application.setAttribute("appilcation_level_variable", connection );

RETRIEVING:

String conn =

(String)application.getAttribute ("appilcation_level_variable");

Hope this helps!

-M

nmhb23a at 2007-7-9 20:36:03 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

It depends on what the "something" is IMHO.

For configuration type objects (things that everybody needs access to, but there's really need for only one instance of the data), I like to use the Singleton pattern.

For things that are expensive to use (like database connections) use a Pool or Cache which uses the Factory pattern to fetch instances of the object for use.

rweavera at 2007-7-9 20:36:03 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

I like to have something like Configurator singleton object with public static Something getSomething() methods etc. It is instantiated at the startup time and is available all across the application.

On resources which are expensive to create (db connections etc) you might find Factory pattern useful: cache the connections and let Factory decide wether to return newly created COnnection or a cached one. In that case you'll have something like Factory.getInstance().getConnection() where getInstance() is public static, and getConnection() is instance level method.

dcengijaa at 2007-7-9 20:36:03 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

I agree, Singleton is the way to go. Here's an example:

public class MyClass {

private static MyClass instance = null;

// notice that the constructor is private

private MyClass() {

}

public static void getInstance() {

if(instance == null) {

instance = new MyClass();

}

return instance;

}

}

// client code

MyClass reference = MyClass.getInstance();

Since the getInstance method is static, it's accessible from anywhere. You can also choose to derive this class and return an instance of a derived class later, with a modified implementation, without changing the client interface.

Dave

CunninghamDa at 2007-7-9 20:36:03 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Your getInstance() is not thread safe. You could add a static initializer that sets the instance variable when the class is loaded. Then getInstance() can simply return the variable without doing a null check. Of course, if you need to free the instance for some reason, an alternative approach is needed.

cknelsena at 2007-7-9 20:36:03 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
And also the method which returns the instance should have the return type of its own class but not VOID
sp77bella at 2007-7-9 20:36:03 > top of Java-index,Other Topics,Patterns & OO Design...