简体   繁体   中英

Unable to access methods which i have created on other Class

I have created 2 classes and I am trying to access the methods from other class by creating an object and trying to call the method, but I am not able to.

Class 1

public class Paramdemo {
    public static void main(String[] args) {
        Paramdemo obj1=new Paramdemo();
        obj1.sub();
        obj2.sum();
    }   
  }     
}

Class 2

public class Paramdemo2 {
    public int  sum (int a, int b) {
        int c = a + b;
        return c;
    }

    public double sum(double d, double f) {
        double a = d + f;
        System.out.println("Print " + z);
        return a;
    }

    public  int sub(int c, int d) {
        int z = c + d;
        return z;       
    }
}

Here:

Paramdemo obj1=new Paramdemo();

but Parademo doesn't have those methods you intend to invoke; Parademo2 has them!

Simple as that - just change the type of obj1 !

And then add the definition for obj2, or rename it to obj1. And finally: include the required parameters for the methods.

You are not able to call these methods because you are creating object of Paramdemo class itselt and Paramdemo class doesn't contain any method. You have to create object of Paramdemo2 class.

Paramdemo2 obj1 = new Paramdemo2();
obj1.sub(10, 5);
obj1.sum(10, 4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM