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
196 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
hibernate insert example 13 34
SHA2 certs for IIS AND Java? 2 113
Java basic valueOf question 1 29
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

820 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