12

Here, I was trying to implement a singleton class for my Database connectivity using the inner static helper class :

package com.myapp.modellayer;

public class DatabaseConnection {

    private DatabaseConnection() {
        //JDBC code...
    }

    private static class ConnectionHelper {
        // Instantiating the outer class
        private static final DatabaseConnection INSTANCE = new DatabaseConnection();
    }

    public static DatabaseConnection getInstance() {
        return ConnectionHelper.INSTANCE;
    }
}

However, my doubt is when does this static inner class, ConnectionHelper, gets loaded in to the JVM memory:

At time when DatabaseConnection class gets loaded, or At a time when getInstance() method is called ?

Amitesh Rai
  • 866
  • 11
  • 21
  • 2
    Note that static in `static class` does *not* have the same meaning as static in a member declaration in Java. It simply means it is a [nested *non-inner* class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class). – user2864740 Aug 07 '14 at 21:34
  • @user2864740 BTW the answer there is just as wrong as 99.9% of all others on the subject. There is actually such a thing as an inner class *without* an enclosing instance. (See [here](http://stackoverflow.com/questions/20468856/is-it-true-that-every-inner-class-requires-an-enclosing-instance), for example). – William F. Jameson Aug 07 '14 at 21:41
  • 1
    There is nothing particularly special about static nested classes. They are loaded the same way as non static and/or non nested classes. To the JVM, all classes are the same and it doesn't really understand nesting the way Java does. – Peter Lawrey Aug 07 '14 at 21:46

2 Answers2

11

When the class gets loaded is just an implementation detail; you want to know when the class is initialized. It will get initialized only when it is first needed, and that is when you call getInstance().

You are BTW using the lazy initialization holder class idiom which is based on exactly this guarantee by the Java Language Specification. As Josh Bloch said,

This idiom is almost magical. There's synchronization going on, but it's invisible. The Java Runtime Environment does it for you, behind the scenes. And many VMs actually patch the code to eliminate the synchronization once it's no longer necessary, so this idiom is extremely fast.

William F. Jameson
  • 1,833
  • 9
  • 14
  • I read about this thread safety mechanism being widely used in Singleton design pattern somewhere, but wasn't sure about the loading of the classes – Amitesh Rai Aug 07 '14 at 21:43
5

The oracle doc page says:

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

You it is loaded the same way other classes are loaded.

rupesh jain
  • 3,410
  • 1
  • 14
  • 22