reflection « string « Java Data Type Q&A

1. A way to use a string for creating new classes

I have a string with the name of a class, is there a way (writing in Java) to use that string when creating a new instance of that class, to do ...

2. Parsing Objects from String in Java

I am trying to write a general method to parse objects from strings. To be clear, I have the following not-so-elegant implementation:

public static Object parseObjectFromString(String s, Class class) throws Exception {

3. Getting Class type from String

I have a String which has a name of a class say "Ex" (no .class extension). I want to assign it to a Class variable, like this:

Class cls = (string).class
How can ...

4. How to call Java function from string stored in a Variable

Possible Duplicate:
Calling a method named “string” at runtime in Java and C
I need to be able to call a function, but the function name ...

5. How can I know if Object is String type object?

I have to know if Object is String or any other class type, how can I do it? Currently I do it like below, but its not very good coding.


6. Run piece of code contained in a String

I have a piece of Java code in a String.

String javaCode = "if(polishScreenHeight >= 200 && " +
    "polishScreenHeight <= 235 && polishScreenWidth >= 220) { }";
Is ...

7. Java. String contents to create an object's instance

I know this is part of java.lang.reflect.* but I wasn't able to find any suggestions to point me how to tackle this. I want to use the contents of a string to ...

8. Loading a Class from a String

I want to instantiate a class by the value of a String. I found several tutorials that show several methods for doing this. The class MUST inherit from a ...

9. Java reflection: getMethod(String method, Object[].class) not working

I have the following code:

public void myMethod(Object... args) {
    System.out.println("this is myMethod");

public void invokeMyMethod() {
    Method s = this.getClass().getMethod("myMethod", Object[].class);
    Object[] ex ...

10. Does reflection require that literal strings are written among the bytecode?

When Java (or any language capable of reflection) outputs a program, does it keep the names of the methods as strings within the bytecode? I'm wondering how, for example, the following is ...

11. Can I condition on a variable being of type String?

Is there a way to do this in java? If variable is a String do one operation, otherwise perform another operation. For example, given variable a

if a is a string

12. What is the purpose of modifying a string using reflection?

I was reading an article that said that Java strings are not completely immutable. However, in the article's sample code that modifies the string, it makes a call to ...

13. effect of changing String using reflection

As we all know, String is immutable in java. however, one can change it using reflection, by getting the Field and setting access level. (I know it is unadvised, I am ...

14. String Mutable with Reflection

Actually Raj is right. With reflection you can get access to the private char[] value, and modify its contents. You can even modify multiple "different" strings that way, if they share this same char[]. Once you start using reflection to access non-public members, many of the guarantees made by any class are immediately void. That includes the immutability of String.

15. Reflection determining return Type as String?

This feels like a dumb question..but..How can determine the Object type being returned from a method? I have tried: where when I call this method with a class and a function that returns and Integer.. it never falls into the Integer section? what mechanism am I supposed to use to determine the type of t? (In the debugger the value fields ...

16. java.lang.Reflection throwing error when using (String)field.get(new String());

Hello, I have a class in with variable definitions, which I want to read with Reflection: public class Categories { private long id; public String name = "abc"; } and I want to read the value of the String with the following program called : import java.lang.reflect.Field; public class Reflect { static private Field[] fields; public static Field[] createFields(Object ...

17. how to read string value using java reflection

found issue,now getting value.. File file = new File(); Class aClass = file.getClass; try { Field field = aClass.getField("sortColumn"); String fieldName = field.getName(); field.setAccessible(true); value = (String) field.get(file); System.out.println("@@@@@@@@@@@@@@@@@-field value-@@@@@@@@@@@@@@@@@@"+value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } Class aClass = this.getClass; try { Field ...

18. calling method indexOf using reflection?

Hi guys, Need help. is it possible to invoke indexOf method using reflections.. actually im trying to write a program similar to beanshell, where user input will be read as string and using reflection im calling the methods. so is it possible to call "indexOf" method using reflection, if yes please tell me how to do this... for example: if user ...