Constructor Inside Inner Static Class in java?
Asked Answered
G

2

12

I Wrote the following Code

public class Reader1 {
    private int pageNumber;

    private class ReaderName1{
        public int getPage(){
        return pageNumber;
        }
    }
    static class ReaderFound{

    }
}

When I used the Java Class File Disassembler javap on the compiled code I got

1. for Reader1.class



class Reader1$ReaderName1 {
              final Reader1 this$0;
              private Reader1$ReaderName1(Reader1);
              public int getPage();
            }


2. for Reader1$ReaderName1.class



    public class Reader1 {
          private int pageNumber;
          public Reader1();
          static int access$000(Reader1);
        }

3. for Reader1$ReaderFound.class



     class Reader1$ReaderFound {
          Reader1$ReaderFound();
        }

My question is since ReaderFound is a static class how can it have an default constructor ? If so why ? Is it allowed ?

If allowed what kind of constructor is this that is found inside the class Reader1$ReaderFound because it can't be static. (Also since constructor are implicitly called to initilize an object and since ReaderFound is an static class so there would we no object of it. My point for first question)

Gerundive answered 25/3, 2015 at 15:45 Comment(4)
What do you think a static class is? Why do you think so?Polygyny
All classes have constructors, and they'll be default if you don't declare one that's non-default.Hydra
static class means that you can instantiate this class in every context (not necessarily bound to a Reader1). Non-static classes are always bound to an instance of Reader1.Sankaran
@SotiriosDelimanolis What i have understood for static class is its behaves just like any top level class but is written inside some another class for some convention purpose ( Enlighten me on this if i've misinterpreted ) but i was really confused over static term, since for public class there was public constructor, for private there was private constructor, So what kind of default constructor this class will have ?Gerundive
F
18

There are four kinds of nested classes in Java: static member classes, nonstatic member classes, anonymous classes, and local classes.

A static member class can be seen as an ordinary class that is declared inside another class. It does not need an instance of the enclosing class to be instantiated. Since no instance is required, it has no access to instance methods and variables from the enclosing class. It is able to access class members though, even if they are private.

class EnclosingClass {
    static class StaticMemberClass { }

    public static void main(String... args) {
        EnclosingClass.StaticMemberClass nestedInstance =
            new EnclosingClass.StaticMemberClass();
    }
}

Being a static nested class does not mean you can't create an instance from it. Thus, a default constructor will be created if you do not create a custom one.

Flyleaf answered 25/3, 2015 at 16:1 Comment(2)
But what is the use of creating an object of a static class ? We can always access it's methods by Outer.Inner.foo() ?Agentival
@BreakingBenjamin Only if the method itself is static. If not, you need an instance to access it.Flyleaf
P
0

One example use of static nested classes is the the builder-pattern. You declare the builder as a static nested class. So it has a access to private fields of the enclosing class but the builder can normally instantiated.

Peabody answered 12/6, 2024 at 6:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.