wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private access problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private access problem" Watch "private access problem" New topic
Author

private access problem

Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

Examlab:


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?


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

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?
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

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
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.

Anyone has a good explanation ?

SCJP 93 % ,preparing for SCWCD
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Error is at line no 3 and 4.
because here we are accessing a variable which is private for class A and private means accessable in same class only.


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi All,

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
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Sudhakar Sharma wrote:Hi All,

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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

I hope it may be clear now..

Happy Learning.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi Shanky,

Now i understand the implications clearly.

Thank You! for the crystal clear answer

Thank You Again.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

You are Welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: private access problem
 
Similar Threads
private static inner class
class level access modifier
Private Field
Limiting creation of an object by a single class
default values