0

I have this method:

 public static Object filterObject(Object object, String objectName){
      ...
 }

And here is how to I call it:

Entity1 entity1 = new Entity1();
//Call some setters
Test.filterObject(entity1, "Entity1");

I want to cast Object into Entity1 inside of that method. How can I do this?

Edit My question is how can I convert object into an instance of objectName class?

hamed
  • 7,939
  • 15
  • 60
  • 114
  • 2
    You won't lose any data by casting objects. It will either work or throw a ClassCastException if the object cannot be casted. – Codebender Aug 08 '15 at 06:06
  • If the method needs to treat the object as an instance of `Entity1`, why not just change the parameter type? – Kevin Krumwiede Aug 08 '15 at 06:09
  • @KevinKrumwiede Because `Entity1` is just an example. I want to cast it to `xxx`. – hamed Aug 08 '15 at 06:10
  • 1
    @hamed, if you say what you want to do by casting, people may be able to help you. Just casting all object's to their respective classes wont make much sense. – Codebender Aug 08 '15 at 06:17
  • What are you planning to do *after* the cast? There's nothing you can do that will actually compile, without using the actual class name as such. Are you by any chance looking for Reflection? Or Genrics? – user207421 Aug 08 '15 at 06:23
  • possible duplicate of [Cast Object to Generic Type for returning](http://stackoverflow.com/questions/14524751/cast-object-to-generic-type-for-returning) – duckstep Aug 08 '15 at 06:26
  • "I want to cast it to `xxx`" - You can't cast it to something that it isn't, so you might as well make the parameter type reflect what it actually is. – Kevin Krumwiede Aug 08 '15 at 19:04
  • Re. your edit: you can't. Casting never changes the actual type of an object. – Kevin Krumwiede Aug 09 '15 at 19:10

3 Answers3

3

You should be using generics.

For example,

public static <T> T filterObject(T object, String objectName) {
  ...
}

Entity1 entity1 = new Entity();
Test.filter(entity1, "Entity1");

By using generics you don't need to cast and can avoid ClassCastException. Basically T can be substituted with your object's type.

Additionally, you can also use the following if you want to guarantee that the object being passed is a subclass of another type.

public static <T extends ParentClass> T filterObject(T object, String objectName) {
  ...
}

EDIT: You should be using generics over casting due to the reasons stated above if you do not need a mixed bag of different types. Refer to this post for a good clarification on whether or not you should be using generics. https://stackoverflow.com/a/11402351/5085407

Community
  • 1
  • 1
Xtrinity
  • 125
  • 1
  • 5
  • Yep! You're right. Don't know why I attempted the cast route – Pumphouse Aug 08 '15 at 06:23
  • Actually, it's because OP asked to cast but I believe you're right regardless – Pumphouse Aug 08 '15 at 06:27
  • Right, but my question is how can I convert `object` into an instance of `objectName` class. – hamed Aug 08 '15 at 06:28
  • See my edit. it's good practice/safer to use generics over casting if what you're doing can be done with generics and accomplishes the same goals as casting. – Xtrinity Aug 08 '15 at 06:34
  • This has nothing to do with the question, which is asking how to cast when only given a class name. – VGR Aug 08 '15 at 19:27
  • @VGR The question is nonsensical then unless OP is explicitly testing to show when casting fails. In an **inheritance model**, you can only cast a subclass to it's parent class or down cast from the parent class to the subclass with the latter being considered bad practice without `instanceof` checks. In which case you're better off using generics along with `instanceof` checks. One possible reason I can think of to not use generics would be if OP is passing an array of objects but that's not the case. Even then however, generics can still be used but are not ultimately necessary. – Xtrinity Aug 08 '15 at 23:15
  • Boxed primitives are an exception to my statement on casting in an inheritance model – Xtrinity Aug 08 '15 at 23:16
1

If at all possible, you should pass the actual class object:

public static <T> T filterObject(Object object, Class<T> desiredClass) {
    return desiredClass.cast(object);
}

If you really must pass a class name, you can use Class.forName:

public static Object filterObject(Object object, String className)
throws ClassNotFoundException {
    Class<?> desiredClass = Class.forName(className);
    return desiredClass.cast(object);
}
VGR
  • 40,506
  • 4
  • 48
  • 63
0

As Codebender told you this is only a matter of specifying the cast:

public static Object filterObject(Object object, String objectName){
    Entity1 entity1 = (Entity1) object;
    // Your implementation here
    return entity1;
}

Another approach is using "generics", e.g.

public static <T extends Entity1> T filterObjectWithGeneric(T object, String objectName) {
    // Your implementation here
    return object;
}

Greetings.