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 4555640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:15:14+00:00 2026-05-21T17:15:14+00:00

Here’s an extract from a REPL session that hopefully explains what I want to

  • 0

Here’s an extract from a REPL session that hopefully explains what I want to achieve:

user> (Integer/parseInt "1")
1
user> (def y Integer)
#'user/y
user> (y/parseInt "1")
No such namespace: y
  [Thrown class java.lang.Exception]

How can I access static method/fields of a Java class using a non-Classname, user defined symbol?

UPDATE

The following works as expected:

user> (eval (list (symbol (.getName y) "parseInt") "1"))
1

Is there a better/more idiomatic way to achieve the same result?

  • 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-21T17:15:15+00:00Added an answer on May 21, 2026 at 5:15 pm

    If you cannot determine the class (possibly programmatically in a macro) during compile time, you need to resort to use reflection. This would do the same thing as eval does when it tries to compile the code. See clojure.lang.Reflector/invokeStaticMethod: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Reflector.java#L198

    (import 'clojure.lang.Reflector)
    ;; Here, you can pass *any string you have at runtime*
    (Reflector/invokeStaticMethod Integer "parseInt" (into-array ["1"]))
    

    This can be used in arbitrary ways at runtime, since it’s not a macro or a special form. For example, the name of the method could be given by the user via a GUI or though a socket at runtime.

    If you have the name of the class at compile time, you can use a macro as Nicolas suggested. However, it’s unnecessary to construct the code to look like (Integer/parseInt "1"), since it’s just syntactic sugar for the more basic (and macro friendly) . special form: (. Integer parseInt "1").

    ;; Here, the method name needs to be a *string literal*
    (defmacro static-call
      "Takes a Class object, a string naming a static method of it
      and invokes the static method with the name on the class with
      args as the arguments."
      [class method & args]
      `(. ~class ~(symbol method) ~@args))
    

    However, the only “real work” this macro performs is to convert the string into a symbol. You would probably just use the . special form in an outer macro (the one that acquires the names of the methods somehow, e.g. by getting those passed as arguments, or by reading them from a var or from a configuration file).

    ;; Use ordinary Clojure functions to construct this
    (def the-static-methods {:foo ["Integer" "parseInt"], :bar ["Long" "parseLong"]})
    
    ;; Macros have access to all previously defined values
    (defmacro generate-defns []
      (cons `do (for [[name-keyword [class-string method-string]] the-static-methods]
                  `(defn ~(symbol (name name-keyword)) [x#]
                     (. ~(symbol class-string) ~(symbol method-string) x#)))))
    
    (generate-defns)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the flow that I am trying to achieve: 1) User uploads an audio
Here is a complete example. I want to forbid using A::set from objects casted
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here's the deal: I'm in the process of planning a mid-sized business application that
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
Here's the method. I want to know if I am violating any best practices
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior

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.