0

If I declare an inner class public. Can it be accessed by another class?

7th May 2017, 3:06 PM
PHI Bui Nguyen Thanh
PHI Bui Nguyen Thanh - avatar
5 ответов
+ 8
classes other than outer class has to access it using outer class e. g. public class A {public class B{} ; public static B b=new B() ;} ; class B would be accessed like this - A.b;
7th May 2017, 3:25 PM
Vaibhav Sharma
Vaibhav Sharma - avatar
+ 7
@Edward can you please answer one more question can solution provided by me create singleton inner class (inner class with only one instance) or even that is semantically incorrect
7th May 2017, 3:55 PM
Vaibhav Sharma
Vaibhav Sharma - avatar
+ 6
What are you mean another class? Inner class available only for outer class containing the inner one.
7th May 2017, 3:13 PM
Alexey
Alexey - avatar
+ 4
@Vaibhav Sharma for static inner class correct. If inner class not static you need to create it instance to accsses.
7th May 2017, 4:01 PM
Alexey
Alexey - avatar
+ 2
if the inner class is private, it can not be accessed outside. if its public, you can access it example: public class Outer and static class Inner in this case you would create an Inner object like this: Outer.Inner inObj = new Outer.Inner (); example 2: public class Outer and class Inner (this time not static) create an inner object: Outer outObj = new Outer (); Outer.Inner inObj = outObj.new Inner();
7th May 2017, 3:28 PM
Edward