2

int.class returns an object of class type.

although there is a method to invoke constructor of a class, but since int.class represents primitive data type so no constructor are available.

Is there any way to create an int variable from int.class object ??

3
  • Reflection may create objects, it doesn't create variables. Commented Dec 9, 2011 at 15:12
  • @DanielA.White: Our Main class contains members of different classes and many primitive data types too. The code takes input from some external entity and converts it into appropriate data type and initializes the value with it. using relfextion, we determine type of the member and continuing on same way, we cast into object or primitive data. Commented Dec 9, 2011 at 15:24
  • Can you give an example where you need to use a primitive with reflection? Commented Dec 9, 2011 at 15:42

1 Answer 1

2

You cannot create an instance of a primitive. Primitives are not Objects by definition. You can create an instance of a wrapper e.g. Integer which has a constructor which takes an int

Sign up to request clarification or add additional context in comments.

3 Comments

so, if i get int.class object, is there any way of getting corresponding wrapper class from int.class itself ?? I don't want to go for hardcoding this stuff.. so, i'm trying to find if any method is available for the same..
There are only 7 (or 8 if you include void) wrappers and its not something you need to do very often because you don't use primitives with reflections.
I suspect you don't need to create such an Integer if you are using reflection as it implicitly converts primitives to their wrappers as required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.