3
private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>() {
    public Connection initialValue() { 
         return DriverManager.getConnection(DB_URL);
    }
};

I don't understand what's going on in the part that's within the stars. Is that a way to insert a method into a class?

blong
  • 2,815
  • 8
  • 44
  • 110
jhlu87
  • 3,999
  • 8
  • 38
  • 48
  • possible duplicate of [What do curly braces in "new" expression mean? (e.g. "new MyClass() { ... }")](http://stackoverflow.com/questions/10468806/what-do-curly-braces-in-new-expression-mean-e-g-new-myclass) –  May 06 '12 at 07:27

5 Answers5

5

This is an anonymous inner class - see http://download.oracle.com/javase/tutorial/uiswing/events/generalrules.html#innerClasses

dfb
  • 13,133
  • 2
  • 31
  • 52
5

The initialValue() method of ThreadLocal is just a way to construct a ThreadLocal holding a value other than null.

Edit: Oh, I see that's not what you're asking about. What you have there is the same as if you did:

public class MyOwnThreadLocal extends ThreadLocal {
    public Connection initialValue() {
        return DriverManager.getConnection(DB_URL);
    }
}

Except your version doesn't require a completely separate class definition--hence it's called an "anonymous class".

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • so if i'm understanding you correctly then the variable connectionHolder is actually not a ThreadLocal object, but a nameless subclass of the Threadlocal object? – jhlu87 Jul 13 '11 at 21:31
  • Exactly correct. If you look at the compiled output, you'll see a class file named "$1.class". That's how anonymous classes are compiled. – Ryan Stewart Jul 13 '11 at 21:36
3

Within the stars you have an anonymous inner class it means :

  • you are embedding a class in a class (so it's an inner class)
  • the inner class is defined within a method body (so it's an anonymous inner class)

it's a syntax shortcut to implement a behavior for an interface or abstract class without declaring a full standard Java class. You are defining a precise behavior for a fixed context.

Next, for the ThreadLocal part,based on the ThreadLocal Javadoc the connectionHolder field is managed by many theads.

Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

If you have 5 threads wich access to the connectionHolder you will found 5 ThreadLocal which managed the 5 instance of the value

Daniel
  • 604
  • 6
  • 7
2

This is an Anonymous Inner Class that are usually implementing some interface that is a callback or something similar, Swing uses this for listeners.

In this case it is implementing the initialValue() method on that particular instance of ThreadLocal<Connection>.

You can override methods on single instances of Objects this way, so if you want just a particular instance to behave in a different way you can override as many methods on that instance you want.

1

This is an anonymous inner class. It is a short syntax for extending a base class:

class AnonClass extends ThreadLocal<Connector> {
    public Connection initialValue() { 
         return DriverManager.getConnection(DB_URL);
    }
}

private static AnonClass connectionHolder = new AnonClass();

You could do the same thing with interfaces (most commonly seen when implementing callbacks). For example:

interface Callback {
    void doIt();
}

Callback test = new Callback() { public void doIt() { /* ... */ } };

Kaini
  • 319
  • 1
  • 8