wood burning stoves*
The moose likes Beginning Java and the fly likes what is the use of this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the use of this " static A a = new A() "" Watch "what is the use of this " static A a = new A() "" New topic
Author

what is the use of this " static A a = new A() "

LeelaKrishna Reddy
Greenhorn

Joined: Mar 14, 2006
Posts: 2
what is the use of static keyword in this statement

static A a = new A();
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
static A a = new A();


means there will be only one instance of Class A per JVM per class loader. You can use this construct to create singleton classes in Java. For example:




So when you call the getInstance method you get the same instance per JVM per class loader.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
The provided example is somewhat contrived. A static field exists once per class per class loader and has nothing to do with what it refers to (the "instances of some class").

You might be better off (for now) conceptualising a static field as "class scoped" as opposed to non-static fields as "instance-scoped".


Tony Morris
Java Q&A (FAQ, Trivia)
LeelaKrishna Reddy
Greenhorn

Joined: Mar 14, 2006
Posts: 2
Hi Tony & Arul thanks for your reply soon.

i have one more doubt!!!
you said if we declare like this static A a = new A(); we can create only one instance for that class but in the below code i created two instances for Singleton class i.e. _instance & st How it is possible ?

class Singleton
{
protected Singleton() { }
static private Singleton _instance = null;
static public Singleton instance()
{
if(null == _instance)
{
_instance = new Singleton();
}
return _instance;
}
void show()
{
System.out.println("Hello...");
}
}
public class StDesign
{
public static void main(String[] args)
{
Singleton st = new Singleton();
st.show();
st = Singleton.instance();
st.show();
}
}



reply soon...
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Singleton pattern is used when you do not want the user of the class to create instances at will. The end user of the class cannot create instances when you declare the constructor as private (and not protected). You just provide a static method using which the end user gets an instance of the class.

In this static method, you can apply any logic. For example, you can create atmost three instances and return null if the user tries to create more instances. If you are applying the Singleton pattern, one can create only one instance and that same instance is returned on successive calls to the static method.

You can create as many instances you want to if you are creating them in the same class. You are doing the same in your previous example.

regards,
vijay.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the use of this " static A a = new A() "