3

this is my code

Class<?> clazz;
    try {
        clazz = Class.forName("classes.Pizza");
        Object p2 = clazz.newInstance();
        System.out.println(p2.test);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Error is "test cannot be resolved or is not a field" I want to get a string containing the class name and create a object with that type.. something like

String x = "Pizza"
x pizza1 = new x();

How do i do that ?

9
  • 1
    cast object to a specific type before you call a method on it. Commented Jun 7, 2015 at 13:31
  • Hint: what is the type of p2 available for compiler? Does this type have test field? Commented Jun 7, 2015 at 13:32
  • 3
    @user3770536 Then how do you know, that "the other class name" has the field test? Or any other field you like to access? Commented Jun 7, 2015 at 13:41
  • 1
    I have a project for school. Basically i created a class Products and other classes (Pizza, Calzone, etc) that have different attributes and i want to export them into txt. And on the import i want to get a attribute named Type which will help me determine what kind of object is that, pizza, calzone or etc to know what objects to create Commented Jun 7, 2015 at 14:03
  • 1
    That's a much better description for your problem. Clearly, you're not going to read or write any Java class to your text file: you only care about Pizza or Calzone. Those classes could implement a common interface with a method void import(String fields) and String export() to read their fields from a string or write fields to a string. They are responsible for correctly reading/writing their own fields, so you don't have to worry about that outside of those classes. I'm skipping over a lot of details here, but I feel like I'm already getting a bit off-topic... Commented Jun 7, 2015 at 16:00

1 Answer 1

2

You have to cast Object to Pizza object:
This could cause ClassCastException too:

Class<?> clazz;
try {
    clazz = Class.forName("classes.Pizza");
    Object p2 = clazz.newInstance();
    System.out.println(((Pizza)p2).test);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (ClassCastException e) {
    e.printStackTrace();
}

EDIT: You cant access the field test, when you dont know about it. So you can access the field:

Class<?> clazz;
try {
    clazz = Class.forName("classes.Pizza");
    Object p2 = clazz.newInstance();
    /*System.out.println(((Pizza)p2).test);*/
    System.out.println(clazz.getDeclaredField("test").get(p2));
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}

Writed by javadoc only, not tested (and not sure about exception in this case)

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

4 Comments

yes. With cast it works.. but i want to create object according to the String x. I only have that string so i can't hardcode the cast to Pizza because that string can be another class
@user3770536 How will you be sure that class has a test field? What should happen if the class with name x doesn't have a test field (e.g. what if x equals "java.lang.Object")?
It will throw an NoSuchFieldException @MattiasBuelens
@maskacovnik I know that's what will happen. I'm asking if that's what OP wants to happen. Sure, you can abuse reflection to get away with anything, but most of the time it's a band-aid solution for the wrong problem.

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.