Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:12:38+00:00 2026-05-29T06:12:38+00:00

In Clojure, how to use a java Class that is stored in a variable?

  • 0

In Clojure, how to use a java Class that is stored in a variable?

How should I fix the following code?

(def a java.lang.String)
(new a "1"); CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: a

And why this one works fine?

(def a str)
(a "1")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:12:39+00:00Added an answer on May 29, 2026 at 6:12 am

    The most elegant solution is to write construct that does the same as new but is able to receive a class dynamically:

     (defn construct [klass & args]
        (clojure.lang.Reflector/invokeConstructor klass (into-array Object args)))
     (def a HashSet)
     (construct a '(1 2 3)); It works!!!
    

    This solution overcomes the limitation of @mikera‘s answer (see comments).

    Special Thanks to @Michał Marczyk that made me aware of invokeConstructor answering another question of mine: Clojure: how to create a record inside a function?.

    Another option is to store the call to the constructor as an anonymous function. In our case:

    (def a #(String. %1))
    (a "111"); "111"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any Clojure http libraries that use the java.net.URLConnection class? Reason I'm asking
I'd like to wrap java's PriorityQueue class in clojure for use in another part
Begin Edit: Primarily I want my Clojure program to use a Java class. I've
user=> (use '[clojure.contrib.duck-streams]) java.io.FileNotFoundException: Could not locate clojure/contrib/duck_streams__in it.class or clojure/contrib/duck_streams.clj on classpath: (NO_SOURCE_FILE:0)
I want to use JOGL (for Clojure, not Java). There seems to be quite
Simple question. I'm new to Clojure. How can I use one file from my
Jar is a convenient way to use java, as every class can be included
When I paste this code into a REPL, it works fine: (use 'clojure.contrib.seq-utils) (defn-
I'm trying to use some Java-classes with Clojure. I've tried it with Scala with
(ns db-example (:use [clojure.contrib.sql :only (with-connection with-query-results)] ) (:import (java.sql DriverManager))) ;; need this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.