Solved

Have a Class object, and a static method within that class. How do i call that method from another class?

Posted on 2006-11-02
9
193 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:dannertb
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:dannertb
Comment Utility
Later edit: the method is not static.
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
  public someMethod()
     {
           ref.aStaticMethod();  // There you go!
     }

or:

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

;JOOP!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
Comment Utility
> i have a Class object that points to myClass. How do i call the static method of that class
Through reflection

Class c = MyClass.class;
Class[] parameterTypes = new Class[] {String.class, ...};
Method m = c.getMethod("method-name", parameterTypes);
Object result = m.invoke(null, arguments); // null since you are calling a static method
0
 

Author Comment

by:dannertb
Comment Utility
Bravo hoomanv. The only one who understood the real problem.
0
 

Author Comment

by:dannertb
Comment Utility
However the method is not static...
0
 
LVL 14

Expert Comment

by:hoomanv
Comment Utility
> 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
0
 

Author Comment

by:dannertb
Comment Utility
Yeah, with newInstance().

Thanks.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now