2

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());
}

}

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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());
    }
}

Comments

0

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);

2 Comments

you are right i couldn't explain it properly. the thing is, getTwoName() method is exists in many different classes. So, I'm trying to access this method by simply passing class name or Object of that class.
@MayurMaisuria In that case you are going to need Reflection as per my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.