• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the use of static keyword in this statement

static A a = new A();
 
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
LeelaKrishna Reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!