*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "modifiers" Watch "modifiers" New topic
Author

modifiers

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
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.


The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You cannot access a private member outside of the class it is declared in. Otherwise which purpose would a private member serve?
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Jan 18, 2002
Posts: 75
Many thanks,
Rowan.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Uday Kumar
Greenhorn

Joined: Jan 22, 2002
Posts: 24
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Feb 26, 2001
Posts: 4456
    
    6

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);


Junilu - [How to Ask Questions] [How to Answer Questions]
 
Consider Paul's rocket mass heater.
 
subject: modifiers