No, local inner class cannot be declared as private or protected or public because local inner class is not associated with Object.
Example of local inner class
public class Main{ private String website="826.a00.myftpupload.com"; void show(){ class LocalInnerClass{ void display(){ System.out.println(website); } } LocalInnerClass localInnerClass = new LocalInnerClass(); localInnerClass.display(); } public static void main(String args[]){ Main obj=new Main(); obj.show(); } } |
Output
w3schools.com |
Above of local inner class with public access specifier
public class Main{ private String website="826.a00.myftpupload.com"; void show(){ public class LocalInnerClass{ void display(){ System.out.println(website); } } LocalInnerClass localInnerClass = new LocalInnerClass(); localInnerClass.display(); } public static void main(String args[]){ Main obj=new Main(); obj.show(); } } |
Output
Main.java:4: error: illegal start of expression public class LocalInnerClass{ ^ 1 error |
Note: Same case will be for private and protected access specifiers.