This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes print variable in other class?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "print variable in other class??" Watch "print variable in other class??" New topic
Author

print variable in other class??

hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54

i have created an object for class two.
so, now the compiler calls the constructor for class two, which in turn calls constructor of class one, where i have initialized the variable 'var'.
now i have tried to print the value of variable var using the object 'obj' of class two.
i expect the output to be 10. but i get the compiler err as symbol not found.
i do not know where my concept went wrong... please explain
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
You've declared int var as a local variable in the constructor of the one class, so once that constructor finishes running the variable is out of scope. Try making it an instance variable (declare it outside your constructor) and you should have better luck.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
var is local to the one constructor, so as soon as you leave the one constructor it no longer exists. If you want to retain it you will have to make it an instance variable in the one class.


Joanne
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by hari harann:

i have created an object for class two.
so, now the compiler calls the constructor for class two, which in turn calls constructor of class one, where i have initialized the variable 'var'.
now i have tried to print the value of variable var using the object 'obj' of class two.
i expect the output to be 10. but i get the compiler err as symbol not found.
i do not know where my concept went wrong... please explain



Hey !

When you declare some thing like this , its local to the constructor block !

How can you access it outside ?

Its never accessible even ' outside the block '

Watch this code :

Even printing the 'int var' using the object of same class is not possible
dude.

one obj1 = new one();

sop(""+obj1.var); // totally not allowed by the language construct !






Even if you say that you inherit one from two

All class members

-static variables
-static methods

All instance Members

-nonstatic variables
-nonstatic methods

are the only things accessible.



All variables / fields that are declared in the constructor block or the method block or a static block is not allowed to be accessed outside.

Include Private members too in that list.

When you say extends we extend the access to the behavior's(methods) of the class and nothing else.

You went wrong in understanding 'extends' i hope !

This is very normal in learning java !

More over this is the way we learn java !

You're Welcome !

Hope, this helps.
[ August 08, 2008: Message edited by: ram kumar ]

Discussion - the powerfull way to excellence!
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Thanks ram kumar.. i have understood the whole concept which i did not know all these days..that helped me a lot to answer so many unanswered quesions of mine
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by hari harann:
Thanks ram kumar.. i have understood the whole concept which i did not know all these days..that helped me a lot to answer so many unanswered quesions of mine


I would recommend you to use Thinking in java ! - Bruce Eckel

The way its written suits best to beginners.

Before you touch that learn the basics of OOPs.

I would recommend the first 20 pages of E.Balaguruswamy - ObjectOriented progamminng with c++.

You will get what is what on oops , theoritically!

Then you could go for Java , programatically !
 
wood burning stoves
 
subject: print variable in other class??