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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:42:38+00:00 2026-06-09T17:42:38+00:00

As you will see below. I am writing a class named Property that can

  • 0

As you will see below. I am writing a class named Property that can be bound to any Serializable type as evident from the class description.

Now the value in the property is auto bound to be of type T during compilation.

I want to implement a Class getType() method that should return the Class object of the value at runtime i.e.

Property<String> p = new Property<String>();

Class<String> cl = p.getType(); 

Here I expect cl to be String.class. Of course one way is:

return value == null ? null : value.getClass();

The issue is it won’t reflect in the type returned and returns a raw type of Class object.
Ideally I want it to be of type Class<String>

public class Property<T extends Serializable> implements Serializable {

    private T value = null ;
    private String name = null ;
    private boolean dirty = false ;
    private Entity parent = null ;

    public Class getType() {
        // Here I want to determine the type of T that this object is bound to ?
        return class; 
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value; 
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDirty() {
        return dirty;
    }

    public void setDirty(boolean dirty) {
        this.dirty = dirty;
    }

    public Entity getParent() {
        return parent;
    }

    public void setParent(Entity parent) {
        this.parent = parent;
    }
}
  • 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-06-09T17:42:39+00:00Added an answer on June 9, 2026 at 5:42 pm

    It is not possible the way you are doing it due to type erasure. One consequence is that this the type that is used to instantiate the type parameter cannot be directly determined at runtime.

    Here are a couple of alternatives:

    1) Use getClass() to get the type of the value of the Property.

        public Class getType() {
            return value.getClass();
        }
    

    2) Explicitly pass the Class object for the actual type of T as constructor parameter. Note that the generic typing means that you can’t accidentally pass the wrong Class object

        private T value = null;
        private Class<T> type;
    
        public Property(Class<T> type) { this.type = type; }
    
        public Class<T> getType() { this.type; }
    

    There is another approach (which probably won’t work here from a design perspective) where you reify the Property classes; e.g.

        public class IntegerProperty extends Property<Integer> {
            public Class getType() {
                returns Integer.class;
            }
        }
    

    There are clever variations of this where the subclass of the generic class in an anonymous class, and/or you access the type parameter via getClass().getTypeParameters(). But note that the getTypeParameters() approach only works if you’ve extended a generic class with specific types for the type parameters.

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

Sidebar

Related Questions

If you look at my document here: http://featuredfotografer.com/ you will see that there is
After running the following SQL statements, you will see that, MySQL has automatically created
If you visit this site , you will see that there is an image
I am working with a legacy C library that can be extended by writing
I am currently writing a program that will eventually compare the files in two
Is there any performance hit for writing a function such that local var statements
I am writing an application in Qt using python that will process a txt
See the code below, If you click on the sub-title row it then will
I am writing a shared library that will allow linked applications to query a
i am writing a function in javascript that will return date array of all

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.