• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Referencing an object instantiated inside a method?  RSS feed

 
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: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show your all code?  thanks
 
Bartender
Posts: 2230
94
Eclipse IDE Google Web Toolkit 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: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • 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.
 
A berm makes a great wind break. And Iwe all like to break wind once in a while. Like this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!