15

I have this classes:

public class User {

    public static final NonRegisteredUser NON_REG_USER = new NonRegisteredUser();

    //...

    public static class NonRegisteredUser extends User {
        //...
    }

}

And code inspector is detecting this warning:

Referencing subclass NonRegisteredUser from superclass User initializer might lead to class loading deadlock

What does it mean exactly?

3
  • I think it is pretty clear what it says. You initialize a NonRegisteredUser in the User class. Every time you load the User class, since the NON_REG_USER is static it will lead to an eternal loop cause the NonRegisteredUser extends the User and so on.. Commented Dec 28, 2017 at 14:44
  • 1
    And so on and so on, and depending on implementation of JVM it may not initialize at all. Commented Dec 28, 2017 at 14:45
  • 1
    It wasn't so clear after all. Commented Dec 28, 2017 at 15:38

2 Answers 2

14

The deadlock can only occur if you have 2 threads and one starts to load User and one starts to load NonRegisteredUser. There are synchronizations in place that will cause a deadlock, but then it requires separate threads. If the loading happens in a single thread, there is no deadlock as the thread owns both locks.

Hence the might in the message. However deadlocks usually do tend to require a specific environment, so there's nothing weird about that.

Sign up to request clarification or add additional context in comments.

Thank you. I'm working on Android, so better I'm gonna solve the warning, just in case.
3

Class loader begins loading User.

Static members are init first, in order of appearance. So the class loader sees the NonRegisteredUser class, and tries loading the User class for its initialization.

Next, class loader begins loading User.

Static members are init first, in order of appearance. So the class loader sees the NonRegisteredUser class, and tries loading the User class for its initialization.

Next, class loader begins loading User.

Static members are init first, in order of appearance. So the class loader sees the NonRegisteredUser class, and tries loading the User class for its initialization...

Thank you. So, why is that code working? Does it make sense?
It's probably working because what I just said is not 100% correct. The compiler build a single init process for the class, so the JVM won't blindely iterates over it over and over during runtime. There is probably some optimization in play here, that fixes this kind of issues.
@Héctor That's a nice question, what happens when you try to use the User.NON_REG_USER attribute or initialize the User.NonRegisteredUser class ?
@giannischristofakis It works fine. But I'm working on android with several threads, the error may occur so I'm gonna decouple the classes

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.