Java equivalent to C# dynamic class type?

12,008

Solution 1

Java doesn't support dynamic typing, but you can simulate something like that using dynamic proxy in Java. First you'll need to declare an interface with operations you want to invoke on your objects:

public interface MyOps {
  void foo();
  void boo();
}

Then create Proxy for dynamic invocation on myObjectInstance:

MyOps p = (MyOps) Proxy.newProxyInstance(getClass().getClassLoader(), //
    new Class<?>[] { MyOps.class }, //
    new MyHandler(myObject));
p.foo();
p.boo();

where MyHandler is declared like this:

public class MyHandler implements InvocationHandler {
  private final Object o;

  public MyHandler(Object o) {
    this.o = o;
  }

  public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
    Method method = o.getClass().getMethod(m.getName(), m.getParameterTypes());
    return method.invoke(o, args);
  }
}

so, if myObject has methods foo() and boo(), they will be invoked, or else, you'll get a RuntimeException.

There is also number of languages that can run in JVM support dynamic typing, e.g. Scala, Groovy, JRuby, BeanShell, JavaScript/Rhino and many others. There is some JVM changes are coming in Java 7 to support a native dynamic dispatch, so these languages could perform much better, but such feature won't be directly exposed in statically typed Java language.

Solution 2

There is nothing like that in Java

Solution 3

There's nothing equivalent in Java. The closest thing you could do is declare a variable of type Object but then you have to cast that variable to whatever you're expecting in order to invoke any method on it that's not implemented by Object (or use reflection but that's sloooow).

Java is a strongly typed language. I think for the next version there will be some dynamic typing, to allow for closures, but that's next year or more probably 2012.

In Groovy you can just use "def" to declare a variable without a type, and the type will be resolved at runtime. And you can compile the Groovy code to Java bytecode...

Share:
12,008
Without Me It Just Aweso
Author by

Without Me It Just Aweso

#SOreadytohelp SOreadytohelp

Updated on July 12, 2022

Comments

  • Without Me It Just Aweso
    Without Me It Just Aweso almost 2 years

    I'm coming from the world of c#.

    in C# i am able to use the class dynamic http://msdn.microsoft.com/en-us/library/dd264741.aspx

    This allows me to not have to use templated/generic classes but achieve a simliar feel for certian situations.

    I'm have been unsuccessfull in internet searchs as unfortunately 'dynamic' and 'java' keywords turn up alot of unrelated infromation on dynamic architectures.

    I have dabbled a bit in javaFX and there is a type var which appears to have the same usage as c#'s dynamic. However it doesnt appear to be usable in Java.

    thanks, stephanie

  • Kirk Woll
    Kirk Woll over 13 years
    what do closures have to do with dynamic typing?
  • Chochos
    Chochos over 13 years
    Probably nothing... I guess it depends on implementation. Using Groovy as a reference, you can declare something with "def myvar" and assign anything to it (including a closure). I suppose you could have a Closure type in Java, but wouldn't it be more flexible if we had dynamic typing also?
  • Benjamin Gruenbaum
    Benjamin Gruenbaum over 10 years
    @Chochos I know this is 3 years late - but for closures what was implemented in C# wasn't dynamic typing - it was type inference.
  • Levi Fuller
    Levi Fuller over 2 years
    11 years later, does Java still not support something similar to dynamic typing?