+ 1

Different ways to use a method from another (external) class

Hi guys, I was wondering what are the different ways to have a class access the method of another class. I know that you can do so in the following way: Method X; //X is the reference name and 'Method' is the name of the method from the other class X = new Method(); //I'm not exactly sure what the technical term for this is but I do know that we seem to be creating a reference for the method here.

31st Jul 2017, 8:25 PM
YodaCoda
YodaCoda - avatar
2 Answers
+ 2
Slight miss-understanding. It is not: Method x = new Method(); Rather, ClassName x = new ClassName(); x.method(); // calling the method So, we aren't creating a reference for the method, rather for the class. Or, if the method is static you can call it from the class directly: ClassName.method(); Note* Since you want to call it from another class the method CANNOT be private. If this isn't Inheritiance it also cannot be protected. In conclusion, the methods are accessed from classes. Whether it be via an object instance or from the class directly. ~~Examples~~ You do this all the time, believe it or not println() is a public method that you are calling from a public static object called 'out'. Which is inside the System class. System.out.println(); // Calling println method Or: Math.pow(); // calling static method 'pow' in Math class Math.sqrt(); // calling static method 'sqrt' in Math class
1st Aug 2017, 12:07 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
It's called creating an instance. We tend to do that what you did. https://code.sololearn.com/cYIUAWE6PLRE/?ref=app
31st Jul 2017, 10:08 PM
Limitless
Limitless - avatar