Java Undefined Object

26,649

Solution 1

By "undefined object as a parameter", I assume you mean that you're looking to write a function that doesn't specify the type of the object in the function declaration, allowing you to only have one function.

This can be done with generics.

Instead of:

static void func(String str)
{
  System.out.println("The string is: "+str);
}
static void func(Integer integer)
{
  System.out.println("The integer is: "+integer);
}

you can have:

static <T> void func(T value)
{
  if (value instanceof Integer)
    System.out.println("The integer is: "+value);
  else if (value instanceof String)
    System.out.println("The string is: "+value);
  else
     System.out.println("Type not supported!! - "+value.getClass());
}

Test:

func("abc"); // The string is: abc
func(3);     // The integer is: 3
func(3.0);   // Type not supported!! - class java.lang.Double

See Java generics for more information.

Solution 2

One way you can do it is pass two parameters. First parameter is the Object you need to pass and second parameter is the indicator of which type of Object you are passing.

public void TestFunc(Object obj1, String type){}

Of course, there can be better ways than using a String, we can use Enums and some other mechanism. You can also use InstanceOf to differentiate if you don't want to pass additional parameter.

Solution 3

By "undefined object" I assume you mean null. You can cast null to a specific type of object and the compiler will know which overloaded method to bind to the call:

public void method(String s) { . . . }
public void method(Integer s) { . . . }
public void caller() {
    method((String) null);
    method((Integer) null);
}

If you have an object of undefined type, you can use instanceof operator to test what type it is, or getClass() to obtain the class object itself. If you have a null value of unknown type, there's not much you can do other than redefining your method signature to accept an additional argument of type Class.

However, if the comment by Dukeling is accurate and by "undefined object" you mean "an object of unknown type", you should look into using Java generics. Generics let you write a single method that works with a range of object types.

public <T> void method(T arg) { . . . }
public void caller() {
    method("String"); // String arg
    method(0);        // Integer arg
}

Start with the Java generics tutorial for more information.

Share:
26,649

Related videos on Youtube

Aaron Priesterroth
Author by

Aaron Priesterroth

Updated on July 09, 2022

Comments

  • Aaron Priesterroth
    Aaron Priesterroth almost 2 years

    I got an arraylist and a method to add an object to the arraylist. Currently im using overloaded methods to differentiate between different kinds of object. Is there a way to use an undefined object as a parameter for the method and differentiate inside of the method what kind of an object it is?

    • Nishant
      Nishant over 10 years
      code? can use var args and apply instanceof check
    • Renjith
      Renjith over 10 years
      i suppose what you mean by undefined object is objects of unknown type...isn't it?