Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print variable in other class??

 
hari harann
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ram kumar
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
hari harann
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic