• 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

Access Modifiers

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic