Simple Problem? (or: why isn't this working?!)

G

Guest

I have two (VERY) simple classes:

---
class Klasse
{
public static void test()
{
System.out.println("Done!");
}
}
---

and

---
class Caller
{
public static void main(String[] args)
{
Class.forName("Klasse").getMethod("test",null);
}
}
---

Now, when I run the Caller class everything finishes just fine and
without any Errors, but the "test"-Method just isn't called. :-(

Where is my Mistake?!

Thanks,
SuSp
 
B

ByteCoder

(e-mail address removed) wrote in @g47g2000cwa.googlegroups.com:
I have two (VERY) simple classes:

---
class Klasse
{
public static void test()
{
System.out.println("Done!");
}
}
---

and

---
class Caller
{
public static void main(String[] args)
{
Class.forName("Klasse").getMethod("test",null);
}
}
---

Now, when I run the Caller class everything finishes just fine and
without any Errors, but the "test"-Method just isn't called. :-(

Where is my Mistake?!

Thanks,
SuSp

You should just call:
Klasse.test();

I don't know what you are doing there, but it could something like
reflection.
 
G

Guest

But Reflection is exactly what I wanted to use...

The purpose I want it for is of course a lot more complicated matter,
but my problem is described in the scenario above.

Thx!
 
V

Vincent Vollers

You are merely acquiring the Method signature. You should invoke it too...

public class ReflectTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("ReflectTest").getMethod("test",null).invoke(null, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void test() {
System.out.println("hello world");
}

}

Regards,
- Vincent
 
B

ByteCoder

(e-mail address removed) wrote in @o13g2000cwo.googlegroups.com:
But Reflection is exactly what I wanted to use...

The purpose I want it for is of course a lot more complicated matter,
but my problem is described in the scenario above.

Thx!

Sorry, I know nothing about reflection, so I can't help you there...
 
P

Patricia Shanahan

I have two (VERY) simple classes:

---
class Klasse
{
public static void test()
{
System.out.println("Done!");
}
}
---

and

---
class Caller
{
public static void main(String[] args)
{
Class.forName("Klasse").getMethod("test",null);
}
}
---

Now, when I run the Caller class everything finishes just fine and
without any Errors, but the "test"-Method just isn't called. :-(

Where is my Mistake?!

Your mistake may be not reading the API documentation enough.

getMethod returns a reference to a Method object, but you
didn't do anything with it. You need to call the Method
object's invoke method.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top