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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:22:41+00:00 2026-05-24T13:22:41+00:00

Would what be a static equivalent of a dynamic String CLASS_NAME = this.getClass().getCanonicalName() Note

  • 0

Would what be a static equivalent of a dynamic

String CLASS_NAME = this.getClass().getCanonicalName()

Note that the following is not what i’m looking for since it specifically refers to a particular class. I’d like to refer to class name outside of a method (ie in the static {} section)

static String CLASS_NAME = Foo.class.getCanonicalName()

THanks.

  • 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-24T13:22:41+00:00Added an answer on May 24, 2026 at 1:22 pm

    I think what you’re trying to do is this:

    public static <T> String getCanonicalName() {
        return T.class.getCanonicalName();
    }
    

    But I’m sure that you’ve already figured out that this does not work. This is because Java generics rely on type erasure. That is, T only exists at compile time. At run time, T is no longer accessible.

    To do this generically, you must use an instance of the type as you did in your first example.

    Edit

    After realizing the true nature of your question, I do not come bearing a direct answer.

    I have seen certain “hacks” like the following

    static String ClassName = Thread.currentThread().getStackTrace[0].getClassName();
    

    This gets the name of the class, but I’m pretty sure it will not be the same as that returned by getCanonicalName(), and I really don’t know of a way to transform the output of getClassName() into the output of getCanonicalName().

    I think it there are is a change to the Java language that would have really helped developers write intuitive code: if some sort of self keyword to could be added refer to the current class it would simplify any scenario like yours, by allowing you to write things like self.class.getCanonicalName(). But, as things stand, we don’t have that, so what you’d like to do isn’t directly possible.

    There are other possible workarounds. For example, you can try something like:

     static String ClassName = null;
    
     public Foo() {
         if(ClassName == null)
             ClassName = this.getClass().getCanonicalName();
     }
    

    But that requires your class to be constructible, which is not always desirable. It’s also not nearly as straightforward as it could be if we had a self keyword.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one expose static variables like this class MyClass: X = 1 Y
In Java, a 'static method' would look like this: class MyUtils { . .
I would like to do the equivalent of the following xaml in code, but
I would like to write a code in Ruby that behaves like this C#
What would be equivalent Objective C for this: template<class T> class Singleton { public:
I have the following code that produces a date string in en-us format. I
I have a control that I would like to be static, but instead of
In Java, this works as expected: public static void testwrite(String filename) throws IOException {
I have the following classes: public class Note { public string Text { get;
Does anyone know why a library initialized within dlopen() would initialize a static variable

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.