This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Referencing an object instantiated inside a method?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all -

I'm instantiating an object inside a method like...

cInvoker = new ClickInvoker(lastOMove);

Now, cInvoker is originally declared outside the method. I'm then trying to reference the instantiated object inside a 2nd method, but I'm finding that cInvoker is null. I think it must be getting deleted once it goes out of scope or something?

Is there anyway to copy the new object so that I can reference it outside of where it was declared?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show your all code?  thanks
 
Bartender
Posts: 2326
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

James Erickson wrote:...Now, cInvoker is originally declared outside the method.


Can you elaborate what "outside" means in this context ? Share your code.

James Erickson wrote:
I'm then trying to reference the instantiated object inside a 2nd method, but I'm finding that cInvoker is null. I think it must be getting deleted once it goes out of scope or something?


If the object is the same, I don't think it should be null.

Here's a short code that changes the value of a variable across method calls:


output:
 
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that the method where cInvoker is instantiated is not getting called before the method where you use cInvoker?  If it's declared outside of any methods, then I don't think it's going out of scope.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!