• 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

why this code fragment is illegal?

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lionel Siau
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... provided the access to the private member occurs within the class complex and not outside of it...
 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,i've bear that in my mind.
 
reply
    Bookmark Topic Watch Topic
  • New Topic