Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

java private member

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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......
 
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Just testing how the [ code] [ /code] works. Take out the space before code and /code to make it work for you too!
 
reply
    Bookmark Topic Watch Topic
  • New Topic