• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

implement singleton pattern on reflected class

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any way by which i can implement the singleton pattern on a reflected class?
i'm obtaining the class name as a string from a properties file and using Class.forName(String) to instantiate a Class object by that name. is there a way to call a static method implemented in this class (e.g. getInstance(), to implement the singleton pattern )?
rite now i'm using the work-around of calling the newInstance method of Class and then typecasting it to the class name, thus making my static
getInstance method redundant.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

Of course this presumes that the getInstance() method takes no parameters and is static.
 
mangesh lele
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx cindy
i almost used the exact same code
one question though, pertaining to the following line in the javadocs:

)]javadocs


If the underlying method is static, the class that declared the method is initialized if it has not already been initialized.


wot does this mean? forgive me if i'm missing some basic funda
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a class is referenced, it is loaded by a classloader. Then it is linked which usually includes resolution of symbolic references in the class.
However the class itself is not initialized until specific events occur, such as invocation of certain reflective methods in the class library.

Initialization of a class or interface consists of invoking its static initializers (�2.11) and the initializers for static fields (�2.9.2) declared in the class.


Java Virtual Machine Specification 5.5 Initialization
 
mangesh lele
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks again
i am soon gonna dive into the world of JVM
 
reply
    Bookmark Topic Watch Topic
  • New Topic