I thought output was 8, but the answer says it won't compile. Why won't it compile? OOConc instantiates its inner class and then passes it out to another class through a public method. What is the exact problem here?
Don't you try to run this code? What compiler says? How can we access the private class' attribute x in another class?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Dieter Quickfend
Ranch Hand
Joined: Aug 06, 2010
Posts: 276
posted
0
it says x is defined in an inaccessible class or interface... But I have no problem in getting the class, just getting X. However, X doesn't have private access.
So that means, with a getter, I can get an A Object, but I don't have visibility on any of A's members? Then what use is the getter?
I think the method public A getA(); should be static because we access the static class reference in that and the variable x is not private (default access) therefore it is accessible in the whole package.
there is no problem in getting the A's instance through public method So the method is okey at that point and in the class A we have x that have default accessibilty
Please Confirm.
Thank You
Mehdi Ben Larbi
Ranch Hand
Joined: Aug 17, 2010
Posts: 70
posted
0
Dieter Quickfend wrote:it says x is defined in an inaccessible class or interface... But I have no problem in getting the class, just getting X. However, X doesn't have private access.
So that means, with a getter, I can get an A Object, but I don't have visibility on any of A's members? Then what use is the getter?
I had the same problem yesterday when i did the final test and i'm unable to understand the problem.
I tried to compile it and it doesn't run but i can't find why.
Sudhakar Sharma wrote:I got it, sorry for misinterpreting the question, but that's really a big question when class is accessible then why not its variable?
Please elaborate
Thank you.
This is how this works..
i think you have studied about access modifier.If not take them seriulsy because you may face a complex logic in a exam and you ends up find the solution for that question but that question simply have a accessibilty problem like this,
Private modifier means private to a class,even if the class is marked a public and we marked its method as private,then method are bound to remain within a class.
You can check it out this by marking your class no-args contructor as private.You are just wondered to see that you are not even able to even extend the class.
But the question is here, inner class is marked as private and we get that from the public accessor method. In the inner class we have variable x which have default access then when inner class's(which is marked as private) object can be accessible outside via the public method then we have object of that type why cannot we access their variable which have default accessibility?
Dieter Quickfend wrote:it says x is defined in an inaccessible class or interface... But I have no problem in getting the class, just getting X. However, X doesn't have private access.
Since, class A is a member of the enclosing class OOConc, you can access the Class A's instance variable in class OOConc. But for a normal top level class, even though the instance variable of the class are public and the class itself defined as private, you can't access any members/variables of that class.
Dieter Quickfend wrote:So that means, with a getter, I can get an A Object, but I don't have visibility on any of A's members? Then what use is the getter?
It's up to you to decide to give the appropriate logic for access modifiers.
But the question is here, inner class is marked as private and we get that from the public accessor method. In the inner class we have variable x which have default access then when inner class's(which is marked as private) object can be accessible outside via the public method then we have object of that type why cannot we access their variable which have default accessibility?
Please elaborate
Thank you
I think you are so much confused in this question.
Lets see step by step.
first see class OOConc
here we are having a class A which is private innner class.
Inner class shares a special relationship with outer class(Please you have to study all those special relationship)
as the class A is private inner class of Class OOConc
So the Class OOConc(outer class) is eligible to make the object of private Inner Class A
see what getA() is doing it is just creating a object of Private inner class A and returning it.
now come to class AnotherDemo
here we have this line where the magic is happening
break it into pieces new OOConc(). get A() .x=8
new OOConc(). creates a object of class OOConc this object will be created only and only if the class OOConc is in the same package otherwise compiler error will be thrown
now
new OOConc(). getA() the object that is created at runtime will call the getA() method,that method will return the object of private static class A which is perfectly fine.
public A getA(){
return new A();
}
this is working because the object is created in class OOConc,and is return by a method to another class.
error is here
new OOConc().getA().x=8;because now we are try to access a variable of a class which is private and not accessable to outside the class A
Even this is not correct.
new OOConc.A --because class A is a private class and is not visible outside A;
This question will be more clear once you will study the inner classes.