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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:23:32+00:00 2026-05-29T16:23:32+00:00

I have a code: Model.java: public abstract class Model <T> { public static <T>

  • 0

I have a code:

Model.java:

public abstract class Model <T> {
  public static <T> T find(int id) {
    T result = (T) blackMagicMethod(T.class, id);

    return result;
  }
}

, User.java

public class User extends Model<User> {
}

, Main.java:

public class Main {
  public static void main(String[] args) {
    System.out.println(User.find(1));
  }
}

, blackMagicMethod:

public Object blackMagicMethod(Class clazz, int id) {}

The line blackMagicMethod(T.class, id) don’t work, like any hacks described in Getting the class name from a static method in Java.

How can I make this code working?

  • 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-29T16:23:34+00:00Added an answer on May 29, 2026 at 4:23 pm

    The class of a generic type is not available in runtime, i.e. T.class does not make sense.

    The generic types gets translated to Object on compilation. This is what’s called Type Erasure.

    If you really need the class of the type argument, you’ll need to add that as an argument:

    public abstract class Model <T> {
        public static <T> T find(Class<T> clazz, int id) {
            T result = (T) blackMagicMethod(clazz, id);
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My directory structure is: ABC/src/com/example/model/a.java ABC/src/com/example/web/b.java Code for a.java : package com.example.model; public class
I have an .obj parser, code is: class Model { public: List *coords; List
I have an Java class with a static final method getAll: public static final
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I have a method with the signature public void setFoo(int newFoo) in a model
I have a question regarding the Java Memory Model. Here is a simple class
In trying to write more testable Java code, I have been using the Model-View-Presenter
I have the following controller @Controller @RequestMapping(/project/view.html) public class ProjectViewController { private static final

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.