Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

dynamically determining an object instance?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having an array list, which of course contains objects, when I get an object out of the list, is there a way to determine the actual instance of the object dynamically at runtime?
For example:
extendedJPanel myPanel = new extendedJPanel();
ArrayList theList = new ArrayList();
theList.add(myPanel);
Object o = theList.get(0);
Now what I am trying to do is call a method in extendedJPanel after pulling it out of theList. However, I would like to dynamicly determine that 'o' is the instance object 'myPanel' so that I can do something analogous to:
myPanel.myMethod(variable);
but using the object, or the instance information extracted from it. Is this possible? If so, can someone give me a simple example?
Thanks
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use something like:
if (MyPanel instanceof ExtendedJPanel)
{
// Do something...
}
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would a simple instanceof operation do the trick?
[CODE]
extendedJPanel myPanel = new extendedJPanel();
ArrayList theList = new ArrayList();
theList.add(myPanel);
Object o = theList.get(0);
if( o instanceof extendedJPanel ) {
// if must be, so the cast will be fine
extendedJPanel ejp = (extendedJPanel) o;
ejp.myMethod(variable);
}
This sounds like it should do what you're asking. Otherwise, there is a more OOP way of handling this called polymorphism. The folks in the CodeBarn have a great tutorial on this topic that you may want to check out. It involves creating a base interface where you would define the myMethod(). Then have your extendedJPanel implement the interface. Now you can create arrays of the interface type without knowing what the actual objects are but also be guarranteed that the myVariable() method will always be there. Have a look at JavaRanch's discussion on polymorphism and this excellent code example for more details.
http://javaranch.com/CodeBarn/shapes.jsp
Hope this helps
Sean

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
There are several ways to identify an object's class at runtime.
The most useful would probably be the 'instanceof' operator as described in the answers above.
However, if you need to determine not only what class, but also what instance you are currently holding in the 'o' variable, you need to use the '==' operator.
Consider this:
class Myobj{
public static void main(String[] args){
Myobj m1=new Myobj();
Myobj m2=new Myobj();
Myobj m3=m1;
System.out.println(m1 instanceof Myobj);
System.out.println(m1==m2);
System.out.println(m1==m3);
}
}

This would give this output:
true
false
true
Regards,
Marius
 
I was her plaything! And so was this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic