wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access Modifiers" Watch "Access Modifiers" New topic

Access Modifiers

kumar bangali

Joined: Dec 01, 2001
Posts: 27
Here is a piece of code from an example in RHE
public 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
public static void main(String args[])
complex c1 = new complex(1,2);
complex c2 = new complex(3,4);
System.out.println("before c3");
complex c3 = c1.add(c2);
System.out.println("after c3");
//double d = c3.real;
I have two questions regarding this
a) It compiles but gives a runtime error of java.lang.NoSuchMethodError in thread main. Can anyone explain why?
b)RHE mentions that "any instance of complex can access the private variable real". However if the last line
double d = c3.real
is uncommented the code does not compile. c3 is an instance of complex so I thought it should be able to access its real variable using c3.real
Thanks in advance for your help
Bos Indicus

Bos Indicus
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

a) I compiled and ran this code sample without error.
b) I'm not sure what page you're reading from, but I don't believe your inference is correct. c3 is not an instance of complex. It is a reference to one. As such, it's access is limited by the declared scope of the variables.
A page number would be a nice thing to help clarify. I am sure what the intended statement is simply means that within the methods of the complex case, it's possible to manipulate the values of private variables. In other words, the only way to change these private values is by calling a method that has access to them.
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
power jxz

Joined: Dec 27, 2001
Posts: 3
1) I think the class client will be public
and the class complex will be default
2) u want to get "real" in class client that is not instance of class complex.
kumar bangali

Joined: Dec 01, 2001
Posts: 27
a) Thanks Power jxz. Looks like you are right about the first part. I tried changing the class client to 'public' and complex to 'default' and the code works fine now. I also experimented with both complex and client as 'default' and it still worked fine. The only time it fails is when complex is 'public' and client is 'default'. Makes me wonder if there is any rule that if a public class exists in the source it must be the one in which the 'main' method is defined. Any takers?
b)Regarding the second part c3 appears to be an instance of complex since it is created using the 'new' keyword in method 'add'. For your reference the page no in RHE is chapter 3...page 75. c1 and c2 also appear to be instances of complex and the statements d=c1.real and d=c2.real also fail to compile. Does this mean that instances of complex created outside the complex class can access private variables in it only through accessible(public?) methods?

Bos Indicus
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi Kumar
a) yes main must be in the public class if there is one
b)private variables are only accesible whitin the class in which they were declared.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link: http://aspose.com/file-tools
subject: Access Modifiers