MayurCM
MayurCM

Reputation: 701

how can I Use of "Object Class" to call methods of user-defined classes in java

In these two classes I have passed an object of TestClassTwo in the getName method of TestClass from main method now I would like to call getTwoName method using an object obj. Can some one please help me with that. thanks // Below is the code // class1

package Test;

public class TestClassTwo {

    public static String getTwoName()
    {
        return "2nd";
    }

}

// class2

package Test;

public class TestClass {

public void getName(Object obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException
{
    //  call getTwoName method of TestClassTwo using obj object
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException
{
    TestClass tc=new TestClass();
    tc.getName(new TestClassTwo());
}

}

Upvotes: 2

Views: 4347

Answers (3)

user207421
user207421

Reputation: 311039

Why? You don't need to use any object, as the method is static. Using an object to call this method is futile. Just write TestClassTwo.getTwoName().

[I strongly suspect there is something wrong with your question.]

If you really need to access a static method of an unknown class via an object of that class, this is how you do it:

String twoName = obj.getClass().getMethod("getTwoName").invoke(null);

Upvotes: 0

Jon Newmuis
Jon Newmuis

Reputation: 26530

As the others have said, if you intend to access the method statically, you do not need an instance, and therefore you do not need a parameter in TestClass#getName at all. If you do want it to be an instance method, however, you can do one of three things:

1) Take in the type TestClassTwo in TestClass#getName:

public class TestClass {
    public void getName(TestClassTwo obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        String name = obj.getTwoName();
        // Do something with 'name'
    }
    public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        TestClass tc=new TestClass();
        tc.getName(new TestClassTwo());
    }
}

2) Cast the object to an instance of TestClassTwo, checking the type:

public class TestClass {
    public void getName(Object obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        if (obj instanceof TestClassTwo) {
            TestClassTwo two = (TestClassTwo) obj;
            String name = two.getTwoName();
            // Do something with 'name'
        } else {
            // Handle failure accordingly (throw an exception, log an error, do nothing, etc.)
        }
    }
    public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        TestClass tc=new TestClass();
        tc.getName(new TestClassTwo());
    }
}

3) If you want to allow other classes to have a getTwoName() function, define an interface and take an instance of that interface as a parameter to TestClass#getName:

public interface HasTwoName {
    public String getTwoName();
}

public class TestClassTwo implements HasTwoName {
    @Override
    public String getTwoName() {
        return "2nd";
    }
}

public class TestClass {
    public void getName(HasTwoName obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        String name = two.getTwoName();
        // Do something with 'name'
    }
    public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        TestClass tc=new TestClass();
        tc.getName(new TestClassTwo());
    }
}

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117665

cast the Object (the super class) to TestClassTwo (the sub class):

String name = ((TestClassTwo) obj).getTwoName();

or:

TestClassTwo temp = (TestClassTwo) obj
String name = temp.getTwoName();

Note that using an instance to call static method is useless. Instead, use the class name to call the static method:

String name = TestClassTwo.getTwoName();

Upvotes: 2

Related Questions