I'm trying access to a static inner class method, but I can't find the right way.
I need to write this java code in Clojure:
SessionProperties sessionProperties = SessionProperties.Builder().mediaMode(MediaMode.ROUTED).build();
My code is:
(:import [com.opentok OpenTok MediaMode SessionProperties SessionProperties$Builder]))
(def sessionProperties (.build (.mediaMode SessionProperties$Builder MediaMode/ROUTED))
And this is the error:
java.lang.IllegalArgumentException: No matching method found: mediaMode for class java.lang.Class
I'm using the opentok Java library and I don't understand how to access to mediaMode method.
And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. They are accessed using the enclosing class name. To instantiate an inner class, you must first instantiate the outer class.
Inner Classes As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Accessing the Private Members Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.
Clojure solved this new language library problem by running on the JVM and having interoperability with Java classes. When you use Clojure, you can use Java classes and Java libraries. Clojure builds on the strength of the production-hardened and tested JVM and existing Java libraries.
Your Java code does not work. To fix the remedy, add the new
keyword between =
and SessionProperties.Builder()
. It should be:
SessionProperties sessionProperties = new SessionProperties.Builder()
.mediaMode(MediaMode.ROUTED)
.build();
You can do this in Clojure as follows.
user> (import '(com.opentok SessionProperties$Builder MediaMode))
com.opentok.MediaMode
user> (def session-properties (.. (SessionProperties$Builder.)
(mediaMode MediaMode/ROUTED)
build))
#'user/session-properties
user> session-properties
#<SessionProperties com.opentok.SessionProperties@54fc58ee>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With