File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private access problem

 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you try to run this code? What compiler says? How can we access the private class' attribute x in another class?
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope it may be clear now..

Happy Learning.
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shanky,

Now i understand the implications clearly.

Thank You! for the crystal clear answer

Thank You Again.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic