Calling Java from Clojure
Discover how to invoke Java methods, access fields, and instantiate objects directly from Clojure code.
Clojure & Java: Best Friends!
Clojure runs on the Java Virtual Machine (JVM), which means it can use all the amazing Java libraries directly! This is called Java Interop.
- Access Java classes and methods.
- Leverage the vast Java ecosystem.
- Combine Clojure's power with Java's stability.
This lesson will show you how to call Java code from Clojure.
Creating Java Objects
To use a Java class, you often need to create an instance of it. In Clojure, you can do this using a special syntax.
- Use
(new ClassName args)or the shorthand(ClassName. args). - The
.after the class name is a convenient way to call a constructor.
Example: (java.util.Date.) creates a new Date object.