0

I am able to access a class and its variables, but is there anyway in changing a user defined variable?

e.g:

Class c = Class.forName(theclassname);
    Object o = c.newInstance();
    theclassname t = (theclassname) o;

i can do

t.variable = 1;

but can i do

String v = "variable";
t.v = 1;

in any way?

7
  • 2
    C:\\A.txt is a file name not a class name... Commented Jan 1, 2014 at 19:53
  • C:\A.txt is a text file ... a class file should end with .class ! Commented Jan 1, 2014 at 19:54
  • ow yeah. wops :3 any idea's on the second? Commented Jan 1, 2014 at 19:56
  • 1
    Well you can't do it like that - you need to use reflection, via Class.getField and Field.put etc. Commented Jan 1, 2014 at 20:01
  • The question has totally changed from the one before the edit ... Commented Jan 1, 2014 at 20:03

3 Answers 3

2

You can't use

Class c = Class.forName("C:\\A.txt");

on that way.

This piece of code returns Class by provided name, not the txt file.

Instead of this situation, you have to create class (suppose that is a class named A) and then retrieve Class A over reflection with following code:

Class c = Class.forName("A");

Note that is a class, not a txt file.

---EDIT---

With this edit you are completely changed the question.
Based on the new (edited) question, you can't use reflection as you wrote:

t.v = 1;

You should use this code:

t.getClass().getField(v).set(t, 1);

That will solve your problem.

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

Comments

1

C:\A.txt is a text file ... a class file should end with .class !

Write a class A.java then compile it using

javac A.java

then you'll get A.class then you can load it using

Class c = Class.forName("A");

Note here that "A" is not the absolute or relative path to some file on the filesystem but the name of the resource for that class on the classpath. See the javadoc for Class#forName

9 Comments

i.sstatic.net/eUUjF.png still doesnt work while using Class c = Class.forName("A"); or Class c = Class.forName("C:\\A.class");
@user2871826 You have to have A.class in your class path.
@user2871826 forget about Class.forName("C:\\A.class"); it will never ever work! What is the package of your class, Class.forName("A") assumes that is in the default package.
It is an example so i dont have to write the whole path down, but never the less using the class path to access a .class file which is next to the other .class it is being accessed from it doesn't work.
@A4l it is in the package "Mk01.Server.src.carcass.test" and the class file is called "AExampleTestClass.class" no matter what i put in .forname. it doesnt work.
|
1

You can use reflections to set a field like this

String v = "variable";
// like t.{v} = 1;
t.getClass().getField(v).set(t, 1);

Comments

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.