Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is private static nested class not "private"?

Why am I being able to access "private" nested class outside the enclosing class (first) ?

import java.util.*;
import java.lang.*;
import java.io.*;

public class hi {
    static class first {
        private static class nested {
            void main() {
                System.out.println("HELLO WORLD");
            }
        }
    }

    public static void main(String[] args) {
        first.nested a = new first.nested();
        a.main();
    }

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

From JLS 6.6.1:

Otherwise, the member or constructor is declared private. Access is permitted only when the access occurs from within the body of the top level class or interface that encloses the declaration of the member or constructor.

The hi class encloses the nested class, therefore access to nested is permitted anywhere within the body of hi.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading