Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

modifiers

 
Rowan Chattaway
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks,
Rowan.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Uday Kumar
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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);
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic