Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

help needed !!!

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I have came across with a code which I was unable to understand.
public static something getInst(){
if (null == vinstance){
synchronized(something.class){
if (null == vinstance){
vinstance = new something();
}
}
}
return vinstance;
}
Here what I don't understand is the following line
synchronized(something.class){

What is ment by .class where in the Java API this is mentioned ?
Any help in this matter will be appreciated
Thanks
ARS Kumar
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, its hard to be absolutely certain, since class is obviously a field of something. Given that the method is static, and the variable name, class, I would guess th class field contains the Class (capital "C") of the something class. (Look at java.lang.class if you do't know what a Class is.)
Because the method is static, the synchronization lock is going to use something's class lock. It's like a "static" lock, shared across all instance of the class.
The code itself is simply using a double guarded check. You check once to see if a resource is free, then if it is, you make use of it. The catch is that because the first check is outside the sync block, it could be lost as you transition into that block, so you need to check again inside. By first checking outside, you avoid the cost of needless acquiring the lock (in most cases, anyway).
I hoep this helps.
--Mark
hershey@eyeshake.com
 
ARS Kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark for your time.
My real question was where is the .class originates and how can
we code that inside a program? I also thought that that could be
a field somewhere but unable to find it.
The following link deals with the same question and I have found the answer but still unclear about where is this .class came from ?
http://forum.java.sun.com/read/16789542/qANuIIYYxcvkAAEYX#LR

Thanks
ARS Kumar.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually part of the language specification - you can find it in the JLS here. For a class named "MyClass", "MyClass.class" can be used to refer to the Class object which represents the class. It's a class literal.
 
ARS Kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim that was really helpful.
I have also did some search in the net and found something about
.class. It has been mentioned that this seems to be a faster way of getting the Class object than Class.forName() method.

ARS Kumar.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>It has been mentioned that this seems to be a faster way of >getting the Class object than Class.forName() method.
From what I understand, Class.forName() is used when you're trying to find or create an object based on a String name value. If you can call .class, presumably the class is already in your classpath.
But I could be confused, because I can't understand why I have to use Class.forName() to get a JDBC driver, if the driver is already in my classpath?? Shouldn't it be enough to import it??
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this simple example:

// ********************
public class MyClass{
public static void main(String[] s) {
MyClass m = new MyClass();
System.out.println(MyClass.class); // class MyClass
System.out.println(m.getClass()); // class MyClass
}
}
// ********************
I got almost the same question in our SCJD study group, which motivated me enough to find out what is it.
I did a search in JDK source code. No class as a field anywhere, not in Class class, not in Object class, absolutely no where. It is the keyword class can be used this way, to return the Class instance object of MyClass.
Roseanne


[This message has been edited by Roseanne Zhang (edited December 05, 2000).]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you use Class.forName() to make a jdbc driver available because it does more than just return the class object. Im not sure of the details and someone else can probably fill those in.
From the javadoc:
A call to forName("X") causes the class named X to be initialized.
From the jls:
"20.3.8 public static Class forName(String className)
throws ClassNotFoundException
Given the fully-qualified name of a class, this method attempts to locate, load, and link the class"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic