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
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:dannertb
ID: 17864678
Later edit: the method is not static.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17864688
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
ID: 17864699
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

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

or:

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

;JOOP!
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
ID: 17864720
> 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
ID: 17864728
Bravo hoomanv. The only one who understood the real problem.
0
 

Author Comment

by:dannertb
ID: 17864734
However the method is not static...
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17864744
> 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
ID: 17864776
Yeah, with newInstance().

Thanks.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
null output 3 53
Glassfish admin console not working 1 82
netbeans can not run self signed app 8 34
How to fix  socket closed error 11 59
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

740 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