Link to home
Start Free TrialLog in
Avatar of dannertb
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?
Avatar of dannertb
dannertb

ASKER

Later edit: the method is not static.
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-private

;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!
  public someMethod()
     {
           ref.aStaticMethod();  // There you go!
     }

or:

   public someMethod()
     {
           MyClass.aStaticMethod();  // Ditto, NOT via a reference.
     }

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bravo hoomanv. The only one who understood the real problem.
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
Yeah, with newInstance().

Thanks.