Results 1 to 4 of 4
Thread: Use of keyword instanceof
- 03-13-2010, 09:54 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
- 03-13-2010, 10:26 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
wellif its anything like php, its a conditional operator such as == etc.
eg,
if(SomeSpecificClass instaceof SomeSpecificClassorInterface){
// You knw this class either extends somespecificclass or implements somespecificinterface.
}
Mainly used to make sure modular code aheres to the overall program design. (OOP, MVC etc)
- 03-14-2010, 12:07 AM #3
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
it is quite useful if we use objects. the most remarkable example is swing mouse clicking listener like a
as a rule it is much useful if you cannot predict what object is to be chosen by user etc... something like an object ensurance :cool:Java Code:public void mouseClicked(MouseEvent e) { if(e.getComponent() instanceof JButton){return;} }
- 03-14-2010, 10:35 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
Say you have a method, needing a parameter of an instance of the "MyObject" object.
Then, we have 2 objects, extending the MyObject: YourObject and OurObject. If you want to do different things when receiving a MyObject you can do this as follows:
Java Code:public void myMethod(MyObject mo) { if(mo instanceof YourObject){ //do some stuff. }else if(mo instanceof OurObject){ //do some other stuff. }else{ //mo is an instance of MyObject, no extension. } }
Similar Threads
-
Keyword : volatile
By peiceonly in forum Advanced JavaReplies: 14Last Post: 01-18-2011, 06:15 PM -
this keyword
By coltragon in forum New To JavaReplies: 10Last Post: 03-01-2010, 09:20 AM -
super instanceof Class?
By mikeiz404 in forum New To JavaReplies: 11Last Post: 01-23-2009, 07:23 PM -
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
Use of this keyword
By Java Tip in forum Java TipReplies: 0Last Post: 11-18-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks