0

I was wondering if there is a way to check with the if statement of which class an object is an instance of. I would need something like this:

if(object.getClass().equals("class Circle")) return object.radius;

Is this possible in Java?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Luca Giorgi
  • 910
  • 3
  • 14
  • 28
  • `getClass()` returns a `Class`, not a `String`. Look into `instanceof`. – Sotirios Delimanolis Apr 08 '14 at 21:10
  • 1
    I see JavaScript background here :) – Marko Topolnik Apr 08 '14 at 21:10
  • 1
    try `object.getClass().getName()` with fully qualified class name. – Braj Apr 08 '14 at 21:11
  • 2
    Most of the time, you should look for a way to accomplish what you need with polymorphism, rather than using `instanceof`. See http://stackoverflow.com/questions/4192837/how-does-one-use-polymorphism-instead-of-instanceof-and-why – ajb Apr 08 '14 at 21:11
  • possible duplicate of [How to determine an object's class (in Java)?](http://stackoverflow.com/questions/541749/how-to-determine-an-objects-class-in-java) – Matt Taylor Apr 08 '14 at 21:13

3 Answers3

5

The instanceof operator should do the trick:

if (object instanceof Circle ) {
    return ((Circle) object).radius;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

use instanceof

if(object instanceof Circle)
chad martin
  • 31
  • 1
  • 7
0

Proximally, what you are looking for is instanceof. However, the formulation betrays an inherent lack of understanding of Object-Oriented Programming in a strongly typed language:

//Start with no information about 'object'
if(object instanceof Circle) { //Figure out if it is a Circle
  return object.radius; //If so, do something
}

In reality, in Java, you should already know what functions you're dealing with, at least for most situations:

public void getRadius(Object o) {
  //Any old object may not even be related to a geometric thing; o could be a network socket!
}

public void getRadiusBetter(Shape s) {
  //Shapes may or may not have a radius, but at least we are talking about shapes
}

public void getRadiusBest(Circular c) {
  //By definition, c would have the state for the object's radius
}

For instance, we might define Circular as:

public interface Circular {
  public double getRadius();
}

This guarantees any Circular object will be able to return a radius. In general, you want to formulate your code in this manner, taking advantage of strong typing, rather than assuming no information about the objects you're dealing with (as in Javascript or other weakly typed languages). Otherwise you could be stuck making many if-statements and convoluted logic to determine if you can even ask the appropriate questions of the objects you're dealing with. In Java, feel free to make reasonable checks, but always try to be as specific about your object as possible.

Remember, this:

f(x:TypeA) => y:TypeB

Can be guaranteed in a strongly-typed language, but not in a weakly typed one:

public TypeB f(TypeA x) { ... }

In JS you're dependent on the function doing what it says. In Java it will not compile if it fails.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • Thank you for you fine answer, but I'm still a small amount of classes into Java and I've never even used an interface. I'm sure that what you are trying to say is true and that I should take advantage of polymorphism rather than using a bunch of if-statements to make the code cleaner and easier to manage, but right now I'm trying to finish a programming assignment which is not very clear in what it requires me to do, so my code is a bit of a mess. I think I'd need to start over to write clean and OO-like code, but I don't really have the time for that. Anyways, thank you again for your answer – Luca Giorgi Apr 08 '14 at 21:34
  • I would simply say that if you find your code is a mess, there are reasons. Make sure you target the reasons in your next - or even this - go. Taking advantage of strong typing will reduce your debugging time. – Nathaniel Ford Apr 08 '14 at 22:19