aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Possible error in CJ2CSG? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Possible error in CJ2CSG?" Watch "Possible error in CJ2CSG?" New topic
Author

Possible error in CJ2CSG?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Before I claim this is an error in the book maybe one of you guys could check it out. On page 79 in the Second edition of the Complete Java 2 Certification Study Guide it states about private variables concerning an example:
"The private variable real may only be accessed by an instance of Complex."
To me this seems to state that if you have an instance of Complex even in a subclass that you should be able to access the private variables in Complex. I understand how private variables operate (I hope and realize you can't do this but the above line seems to not make the distinction. I would think it should read that "the private variable real may only be accessed from 'within' an instance of the Complex class" but maybe I'm just wrong here?
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
I think the code example from which that snippet was used actually invoked the private variable in the main method of type Complex, which is within type Complex.
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Ok, here was the actual code:
1. class Complex {
2. private double real, imaginary;
3.
4. public Complex( double r, double i ) {
5. real = r; imaginary = i;
6. }
7. public Complex add(Complex c) {
8. return new Complex(real + c.real,
9. imaginary + c.imaginary);
10. }
11. }
12.
13. class Client {
14. void useThem() {
15. Complex c1 = new Complex( 1,2);
16. Complex c2 = new Complex( 3,4);
17. Complex c3 = c1.add(c2);
18. double d = c3.real; //Illegal!
19. }
20. }

And the comment on 79:
'Line 18 is illegal and will cause a compiler error. The error message says, "Variable real in class Complex not accessible from class Client." The private variable real may only be accessed by an instance of Complex.'
So, I know you can do what they are doing at line 18, but it seems like from the above explanation that if you have an instance of Complex the authors are saying you can access the private variable, (which from what I understand, isn't the case from instances outside of the Complex class). So I'm confused what they mean by the above statement. Maybe I am missing something?
Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
HI,
I'm not sure if this code proves anything. I wrote it to see if the private variables could be accessed or not:

In the Test class I have created an instance of the Access class. Calling the print method of the instance I have created in the main() method of the Test class does actually work.
So I think that confirms what the book says. Please correct me if I'm wrong anybody....

Terry


Raising Flares debut album 'Ignition' out now

http://www.raisingflares.com

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
I think I see what they are trying to say now- that access to the variable 'real' is only accomplished when an instance of Complex is being created through the constructor. When they say in this example that "a private variable may only be accessed by an instance of Complex" they are saying "the private variable real is only accessed when an instance of Complex is created by the implementation in the constructor." By their wording it sounds like if you have an instance of Complex you have access to the variable (apart from using any method or constructor). I see what they are trying to get at now, however.
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
My understand about accessing the private members outside the class is that they can be accessed through the calss' non-private members(constructors and methods). I modified the code snippet provided above by Terence.
HTH
--Farooq
===============================================================
public class TestTry{
public static void main( String []args ) {
Access myAccess = new Access();
myAccess.print();
}
}

class Access{
private static int x = 123;
Access(){
System.out.println( x );
printx();
}
void print() {
System.out.println( x + 1);

}
private void printx() {
System.out.println( x +2 );
}
}
===============================================================


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Possible error in CJ2CSG?
 
Similar Threads
Access Modifiers
Let's get this straight!!!
Basic class design
why this code fragment is illegal?
Object Accessibility