aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why this code fragment is illegal? 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 "why this code fragment is illegal?" Watch "why this code fragment is illegal?" New topic
Author

why this code fragment is illegal?

Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
class complex
{
private double real, imaginary;
public complex (double r, double i)
{
real=r; imaginary=i;
}
public complex add(complex c)
{
return new cmoplex (real+c.real,imaginary+c.imaginary);
}
}
class client
{
void usrthem()
{
complex c1=new complex(1,2);
complex c2=new complex(3,4);
complex c3=c1.add(c2);
double d = c3.real //illegal
}
}
why c3 cannot access its private variable real?
Stefan Koeltze
Greenhorn

Joined: Feb 08, 2002
Posts: 14
Hi!
I think:
You can only access Variable real INSIDE methods of your class complex ...
Thats the sense of private..
Hope it helps...
To access it your way you must make it public.
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
Thanks for reply.
there is a sentence in the book "complete java 2 certification study guide"(page 59): The private variable real may only be accessed by an instance of Complex. how to understand this sentence? I think the c3 is a instance of class complex, isn't it?
Lionel Siau
Greenhorn

Joined: Feb 27, 2002
Posts: 12
Yes I've seen that statement. Any instance of the same class will be able to access it. But not in a different class.
After all, it is private u know!! Ask your self logically. What is a private keyword for if it can be accessed so easily??
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
private means "private" to the class and not to the instance. That is, real can be accessed anywhere within the declaration of class complex and not outside of it, even with a reference to an instance of complex, you are not allowed to access a private member from outside class complex.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Lionel Siau
Greenhorn

Joined: Feb 27, 2002
Posts: 12
HUh? I'm reading the RHE now and it states that instances of the same class can access the private members of each other. Pg 78.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
... provided the access to the private member occurs within the class complex and not outside of it...
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
Maybe I'm not very familiar with these two concepts: class and instance. In my opinion, the class is the definition of a object, and the instance is a implementation of the object, isn't it? i'm really comfused with these. expecailly with those two terms: class variable and instance variable. please help me to make it clear. thanks.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Sean Lee:
Maybe I'm not very familiar with these two concepts: class and instance. In my opinion, the class is the definition of a object, and the instance is a implementation of the object, isn't it?

Yes, that's pretty much correct. You realize you can make as many instances of a class as you want right? In this respect, a class is sort of like a cookie-cutter template that can create a bunch of the same type of object.

i'm really comfused with these. expecailly with those two terms: class variable and instance variable. please help me to make it clear. thanks.


An instance variable (actuall both variables and methods) belongs to a single instance of a class. If I have a class Person:
class Person{ String name; }
Each instance of the Person class will have it's own copy of this variable. That way, each object can maintain a unique value for it. One object can have a name="Bob", and another "Fred."
A class variable is shared by ALL the objects of a class. If I add a class variable to the Person class above:
class Person{
static int numberOfPeople; //class variable
String name; //instance variable
}
First you notice we use the keyword "static" to indicate it's a class variable.
Now, each object shares the variable named numberOfPeople. If one object were to change the value, that change would be visible to ALL Objects of type Person. You could use this particular variable to keep track of how many Person objects had been created.
Class variables have many uses, but the key point is that EVERY object of that class SHARES a single class variable, whereas EVERY object will have it's own local copy of an instance variable.


Rob
SCJP 1.4
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
Thanks!
so, according to your explaintion, how to understand the sentence from the book. "The private variable real may only be accessed by an instance of Complex."
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
And I would add (as before) only within the boundaries of the declaration of class Complex... and not outside because private means private to the class declaration.
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
thanks,i've bear that in my mind.
 
jQuery in Action, 2nd edition
 
subject: why this code fragment is illegal?