Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show your all code?  thanks
 
Bartender
Posts: 2585
124
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: 7108
184
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.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic