• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

modifiers

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm working through 'Complete Java2 Certification'. Can anyone tell me why the following code is illegal:
class Complex{
private double real, imaginary;
public Complex(double r, double i){
real = r;
imaginary = i;
}
public Complex add(Complex c){
return new Complex(real + c.real, imaginary + c.imaginary);
}
}
class Client{
void useThem(){
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
Complex c3 = new c1.add(c2);
double d = c3.real; //Illegal!
}
}
Why is the above line illegal?
Even though 'real' is private, it's being accessed via an instance of Complex.
Any help would be much appreciated.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot access a private member outside of the class it is declared in. Otherwise which purpose would a private member serve?
HIH
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The private members are private for the class, not for an instance. Different instances of same class can access private members of wach other.
In case of class Client, the code double d = c3.real; //Illegal! refers to a private member of another which violates data encapsulation, hence an illegal access error.
Not very sure, but I think there's a question like this in the roundup game.
HTH,
- Manish
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks,
Rowan.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rowan Chattaway:


WHOA!!! Back up! What is this line?!?! Is this legal?
I tried something similar, and this gave me a compiler error (which I expected):

This gives me a compiler error at line 1: "The type named getInstance is not defined" which is what I would have expected. Can anyone confirm this or is what was displayed above legal in some convoluded way?
Thanks,
Corey
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,
You can refer to this link. http://www.javaworld.com/javaworld/jw-05-2001/jw-0504-java101.html
Then search for singleton in the page.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since when do we put the "new" keyword in front of a method invocation expression as in:
NewTest b = new getInstance(); // 1
???
I'd like to know that...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Uday Kumar:
Corey,
You can refer to this link. http://www.javaworld.com/javaworld/jw-05-2001/jw-0504-java101.html
Then search for singleton in the page.


Perhaps I named my method poorly. I wasn't trying to show a faulty implementation of a singleton pattern. In the initial code, there was a line that looked like this:

In this case the method getObject() would return an object. However, you can't preface that with new, correct? I believe the correct code should be:

Am I mistaken?
Thanks,
Corey
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct Corey
the keyword "new" can only be used in class instance creation expressions and in array creation expressions. Here is a good article about the former: class instance creation expressions
HIH
[ January 30, 2002: Message edited by: Valentin Crettaz ]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
Complex c3 = new c1.add(c2);
WHOA!!! Back up! What is this line?!?! Is this legal?


Probably just a typo by the poster or in the book... most likely should be as you showed:
Complex c3 = c1.add(c2);
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic