0

For example, in double-checked locking singleton pattern,

public class Singleton {
private volatile static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
    if (uniqueInstance == null) {
        synchronized (Singleton.class) {
    if (uniqueInstance == null) {
        uniqueInstance = new Singleton();
    }
}
}
return uniqueInstance;
}
}

What's the meaning of "Singleton.class"? Is it an object?

Now I know it's class object, then can we use other objects to synchronize here? Such as "this"?

3 Answers3

4

It represents the Class object of that class. Once you get the Class object, you can do a host of things like get the fields of the class, methods of the class, package of the class and so on.

Most commonly, you will use it to get resources as stream. That is, when you want to retrieve an embedded resource from your jar file. For more detailed information, have a look at the documentation

Run the code below directly at: http://ideone.com/h1czR5

SSCCE

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.lang.reflect.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Class string = String.class;

        System.out.println("Package: " + string.getPackage());
        System.out.println("Fields: " + java.util.Arrays.toString(string.getFields()));
        Method[] methods = string.getMethods();

        for(int i = 0; i < 10; i++){
            System.out.println(methods[i]);
        }

    }
}  

Output:

Package: package java.lang, Java Platform API Specification, version 1.7
Fields: [public static final java.util.Comparator java.lang.String.CASE_INSENSITIVE_ORDER]
public boolean java.lang.String.equals(java.lang.Object)
public java.lang.String java.lang.String.toString()
public int java.lang.String.hashCode()
public int java.lang.String.compareTo(java.lang.Object)
public int java.lang.String.compareTo(java.lang.String)
public int java.lang.String.indexOf(java.lang.String,int)
public int java.lang.String.indexOf(int)
public int java.lang.String.indexOf(int,int)
public int java.lang.String.indexOf(java.lang.String)
public static java.lang.String java.lang.String.valueOf(float)
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • @yuanliu200 You are welcome. Most of the common questions have already been answered on Stack including the more technical ones. It is good to search for them before posting a question. It saves everyone time and effort. – An SO User Nov 28 '13 at 07:17
1

You are synchronizing on the "class" object. The class object contains some data "about the class".

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

For every class in JAVA, there exists an object. That object is a class object. This object is singleton Object and can be fetched by Class object=ClassName.class or Class object=Class.forName('ClassName');


Read this for more details.

For your code synchronized (Singleton.class) means you are locking on the class, so that the static member access is sychronized.

Aman Arora
  • 1,232
  • 1
  • 10
  • 26