| Author |
java private member
|
vipul hasija
Greenhorn
Joined: Sep 10, 2011
Posts: 10
|
|
acessing private member outside class gives an error..
but while implementing it in two ways
1.) using object of a class in other class
2.) using inheritence (in other function)
gives two different errors...why is it so..?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
Welcome to the Ranch!
Two different errors, because there are two different situations. Do you have some example code, and what are exactly the error messages that you get? Do those error messages not sound logical to you; if so, then why not?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Have you got any getXXX or setXXX methods?
If you are using inheritance, you should have all the manipulation of the private members in the superclass, so the subclass does not need access to them.
|
 |
vipul hasija
Greenhorn
Joined: Sep 10, 2011
Posts: 10
|
|
class Roo
{
private String getText()
{
return "FULL";
}
}
1.class Poo extends Roo
{
public static void main(String args)
{
System.out.print(getText());
}
}
error: cannot find symbol
symbol : method getText()
2.class Foo
{
public static void main(String args)
{
Roo r= new Roo();
System.out.print(r.getText());
}
}
error:has private access in Roo
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
The error "cannot find symbol" in the first example means that Java doesn't know what "getText" is. Private methods exist only inside the class that they are defined - so in this case the getText method only exists in class Roo. When you try to use it in class Poo, Java doesn't even know that it exists at that point.
The error "getText has private access in Roo" in the second example means that Java does know that the class Roo has a method called getText, but you cannot call it, because it is private. Note that in the second example you are directly using class Roo, unlike in the first example.
The second error message could have been applicable also to the first one, but the Java compiler looks at it a little differently. Anyway, the important thing is that you get an error message when you try to do something that is not correct according to the rules of the language. It is up to the implementation details of the compiler which exact error message you get. If you are studying for an exam, for example for the OCJP, then you don't need to know details like this - the exam is not going to ask you which exact error message you would get with examples like these.
|
 |
vipul hasija
Greenhorn
Joined: Sep 10, 2011
Posts: 10
|
|
Jesper de Jong wrote:The error "cannot find symbol" in the first example means that Java doesn't know what "getText" is. Private methods exist only inside the class that they are defined - so in this case the getText method only exists in class Roo. When you try to use it in class Poo, Java doesn't even know that it exists at that point.
The error "getText has private access in Roo" in the second example means that Java does know that the class Roo has a method called getText, but you cannot call it, because it is private. Note that in the second example you are directly using class Roo, unlike in the first example.
The second error message could have been applicable also to the first one, but the Java compiler looks at it a little differently. Anyway, the important thing is that you get an error message when you try to do something that is not correct according to the rules of the language. It is up to the implementation details of the compiler which exact error message you get. If you are studying for an exam, for example for the OCJP, then you don't need to know details like this - the exam is not going to ask you which exact error message you would get with examples like these.
thanks......
|
 |
Timothy Oldbean
Greenhorn
Joined: Jun 23, 2011
Posts: 17
|
|
Just testing how the [ code] [ /code] works. Take out the space before code and /code to make it work for you too!
|
If it wasn't for physics and law enforcement, I'd be unstoppable!
|
 |
 |
|
|
subject: java private member
|
|
|