Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

using this and instanceof.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Test(){

Test(){
if(this instanceof ClassName)
{
}}}

What does the bolder statement mean?
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instanceof operator has this general form:

object instanceof type

Here object is the instance of the class and type is a class type. instanceof is the means by which your program can obtain run-time type information about an object.

Hope this helps.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One use for such a statement could be if you would have subclasses. Beware however that this is a bad example! (I'll explain why below).

If you need this kind of functionality, you should really override the makeSound method in the subclasses (make use of polymorphism). The problem with the code above is that the Animal class now contains functionality that really belongs in the subclasses. Besides, because the Animal class needs to know about the existence of all of its subclasses, it's hard to add a new animal: you'd not only have to make a new subclass, but also modify the existing Animal class.

This would be much better:

The use of instanceof is often a code smell (a sign that something is wrong with the design of the program).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:The use of instanceof is often a code smell (a sign that something is wrong with the design of the program).


Except in an equals() method.

@Shankara: One useful additional thing about instanceof:
It only returns true if the object (the left-hand side of the expression) is not null.
Thus, when it does return true, you can generally be assured that the object is safe to use.

Winston

 
Greenhorn
Posts: 1
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't agree with a blanket statement that instanceof is a sign of poor design. It is a standard java operator that can be very useful. Take for an example some object that consumes a different object that inherits from a base class.



However, to the OPs initial question of statements that use 'this' with 'instanceof', I have to agree with some of the other posts. For an instance to have to check its own self for a type smells of sloppy OOD.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Julian Master wrote:I don't agree with a blanket statement that instanceof is a sign of poor design. It is a standard java operator that can be very useful. Take for an example some object that consumes a different object that inherits from a base class.



To my mind, that's precisely the kind of bad design he was talking about. This might be a case for the Visitor pattern, or any of a handful of other approaches, depending on more details of the overall design. I would only write it this way as a quick hack when I knew the hierarchy wouldn't be changing, or for a toy program.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Julian Master wrote:I don't agree with a blanket statement that instanceof is a sign of poor design. It is a standard java operator that can be very useful.


Hi Julian. Welcome to The Ranch!

To be fair, Jesper didn't say it's always a poor design. A "code smell" is something that might be an indication of a poor design. Not definitive, but worth thinking about to see if you can improve things.

And I agree with Jeff, I don't like that particular example - you could rewrite that to have no dependencies on the specific subclases of Pet, which would be much more maintainable.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Julian Master wrote:I don't agree with a blanket statement that instanceof is a sign of poor design. It is a standard java operator that can be very useful. Take for an example some object that consumes a different object that inherits from a base class.


It seems to me that a PetShelter is much more likely to have people that come in looking for a Pet, so wouldn't it be more likely to be:simplistic, but hopefully illustrates the point.

Winston

 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic