• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

class inheritance

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written code similar to what is below.
Can anyone explain to me why the instance of class1 created in class2 can't be referenced in class3?


[ EJFH: Added [code ][ /code] tags to preserve formatting. ]
[ March 26, 2006: Message edited by: Ernest Friedman-Hill ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Variables defined inside of a method are "local variables" -- their names are not visible outside of the method in which they're defined. The variable "class1" is local to the method "main" in Class2, and therefore is available nowhere else.
 
Grant Hackshaw
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Earnest 4 the warm welcome and the fast reply.

So I should declare variable class1 outside of the method, is that the trick if I need to use it in Class3?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, looking at your code and trying to guess what you want to accomplish: you want to call someFunction() and have it operate on a specific instance of Class1, created by Class2. The best way to do this, instead of using member variables like (yuk) global variables, is to give someFunction() an argument -- i.e., define someFunction like



Then you can create a Class1 in Class2.main() and pass it to Class3.someFunction(), like this:



Although this may look like a small change, it's really rather profound. What we've done is to decouple Class3 and Class2. Class3 doesn't need to know anything about Class2, and this makes it easier to write the classes and to maintain them when they need to change. The main reason is that it makes everything easier to understand: you can see what someFunction() does without knowing anything about any other class that might use it.
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic