-4

I have two non-static methods which I want to call in a new method. How do i do this ?

I have tried .methodName() but when I try to compile I receive an error message saying "cannot find symbol - method methodName()"

HaM551
  • 35
  • 6
  • You need an `instance` of the `class` in which the method is defined, so long as you can see the `access modifiers` which define it from the calling method. – Kon Apr 11 '15 at 17:01
  • 1
    Duplicate of [Example of an instance method? (Java)](http://stackoverflow.com/questions/17033673/example-of-an-instance-method-java). OP you really need to start with a basic java tutorial or book, this is very basic stuff that you need to teach yourself first. – tnw Apr 11 '15 at 17:02
  • please go read a java fundamental book – OPK Apr 11 '15 at 17:30

1 Answers1

0
public class TestClass {


    public String firstMethod()
    {
        return "first Method";
    }

    public String secondMethod()
    {
        return "Second Method";
    }

    public static void main(String[] args)
    {
        TestClass object = new TestClass(); // create a object for class `TestClass`
        System.out.println(object.firstMethod()); // access the non-static method by `classname.methodName` 
        System.out.println(object.secondMethod());          
    }

}

Output :

first Method
Second Method
Yuva Raj
  • 3,881
  • 1
  • 19
  • 30