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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:29:59+00:00 2026-05-30T07:29:59+00:00

I have a generic base class: public class BaseLabel<T> extends JXLabel { private static

  • 0

I have a generic base class:

public class BaseLabel<T> extends JXLabel {
    private static final long serialVersionUID = 1L;

    public BaseLabel() {
        System.out.println(T.class.getClass().getName()); //error
    }       
}

and Child class:

public class ChildLabel extends BaseLabel<ChildLabel> {
    private static final long serialVersionUID = 2L;

    public ChildLabel() {

    }       
}

I am getting compilation error.

Is there any way to get the actual class name from the constructor of BaseClass. Here, by actual class I am referring to that class, which I am going to instantiate.

For example I am instantiating ChildClass, ChildClass clz = new ChildClass();

then that println() will print package.ChildClass.

Thanks in advance.


The compilation error is:

cannot select from a type variable System.out.println(T.class.getClass().getSimpleName());

If I call this.getClass.getSimpleName() from the abstract BaseClass‘s constructor. It is printing the ChildClass.

Why?

Is it due to, as I am instantiating ChildClass so this is pointing to the ChildClass‘s object.

  • 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-30T07:30:00+00:00Added an answer on May 30, 2026 at 7:30 am

    Ugly? yes

    import java.lang.reflect.ParameterizedType;
    
    
    public class GenericClass<T> {
    
        public GenericClass() {
            System.out.println(getClass().getGenericSuperclass()); //output: GenericClass<Foo>
            System.out.println(((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]); //output: class Foo
        }
    
        public static void main(String[] args) {
            new ChildClass();
        }
    
    }
    

    child class

    import java.lang.reflect.ParameterizedType;
    
    
    public class ChildClass extends GenericClass<Foo> {
    
        public ChildClass() {
            System.out.println(((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]); //output: class Foo
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
Imagine that I have a generic base class like this: public abstract class AnimalDTO<TA,
In my base class I have a generic method (ideally this would be a
I have a base class which is non-generic with a derived generic class. The
I have generic type that looks like: public class GenericClass<T, U> where T :
I created a generic base class for a WinForm UserControl: public partial class BaseUserControl<T>
If I have a generic class like this: public class Repository<T> { public string
I have a compiler error when trying to set a generic base collection class
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following structure: abstract class Base { public abstract List<...> Get(); //What

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.