dannertb
asked on
Have a Class object, and a static method within that class. How do i call that method from another class?
So I have a class, say myPackage.myClass, and within that class i have a static method.
Now - in another class, say myPackage.myOtherClass, i have a Class object that points to myClass. How do i call the static method of that class, within myOtherClass?
Now - in another class, say myPackage.myOtherClass, i have a Class object that points to myClass. How do i call the static method of that class, within myOtherClass?
You can call a method:
by class.method( );
or by
object-reference.method( );
the first applies to static methods, the second to non-static methods,
although static methods are accessible that way.
All depends on the access: public-protected-package-p rivate
;JOOP!
by class.method( );
or by
object-reference.method( );
the first applies to static methods, the second to non-static methods,
although static methods are accessible that way.
All depends on the access: public-protected-package-p
;JOOP!
And please adjust your writing: a reference does not point to a class but to an object.
A class object? do you mean a static reference?
public class MyClass // Starts with a capital letter!
{
public static aStaticMethod(){ <statements> }
.....
}
public class MyOtherClass // Starts with a capital letter!
{
public static MyClass ref = .....;
public someMethod()
{
ref.aStaticMethod(); // There you go!
}
}
;JOOP!
A class object? do you mean a static reference?
public class MyClass // Starts with a capital letter!
{
public static aStaticMethod(){ <statements> }
.....
}
public class MyOtherClass // Starts with a capital letter!
{
public static MyClass ref = .....;
public someMethod()
{
ref.aStaticMethod(); // There you go!
}
}
;JOOP!
public someMethod()
{
ref.aStaticMethod(); // There you go!
}
or:
public someMethod()
{
MyClass.aStaticMethod(); // Ditto, NOT via a reference.
}
;JOOP!
{
ref.aStaticMethod(); // There you go!
}
or:
public someMethod()
{
MyClass.aStaticMethod(); // Ditto, NOT via a reference.
}
;JOOP!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bravo hoomanv. The only one who understood the real problem.
ASKER
However the method is not static...
> However the method is not static...
So instead of null pass an instance reference to the invoke method.
You can construct an instance from Class object too
So instead of null pass an instance reference to the invoke method.
You can construct an instance from Class object too
ASKER
Yeah, with newInstance().
Thanks.
Thanks.
ASKER