0
If I declare an inner class public. Can it be accessed by another class?
5 Antworten
+ 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;
+ 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
+ 6
What are you mean another class?
Inner class available only for outer class containing the inner one.
+ 4
@Vaibhav Sharma for static inner class correct.
If inner class not static you need to create it instance to accsses.
+ 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();