Search code examples
javastaticstatic-class

Using "this" keyword inside nested static class java


I have a class say A, and a static nested class say B.

public class A {
    public static class B {
        B(Temp x) {
            x.reg(this); // need to pass the nested class reference.
        }
    }
}

Is the above code correct? Can we use this keyword inside nested static class constructor?

Please help me on this. Thanks.


Solution

  • yes, it is. For the runtime, inner classes are just another, separate class. If the inner class is not static it will just have a reference to the outer class, but in your case it's static so not even, so it is exactly as if you created a new class in a new file

    Just make sure that you write "public", not "Public"