+ 3

What is the purpose of using e.g. public static double (double a)?

31st Aug 2017, 4:37 PM
黄瀬涼太
黄瀬涼太 - avatar
2 Respostas
+ 2
You'll have to enter a name for your method, but the thing here is the static key word. You want to use this if when the method doesn't depend on a instance, a calculation for example. If you don't need a variable from the instance, you van make it static som you don't have to create an instance when you want to execute the code but just by typing: ClassName.methodName (d);
31st Aug 2017, 5:30 PM
Mark Van Wijk
Mark Van Wijk - avatar
+ 2
public means the method is accessible to the class and outside the class. static means the method can be accessed without an object. For example, if that method is a class called car, there will be no need for Car obj = new Car (param) but you can go ahead and write methodName (param). double over here is the return type, meaning this method will return a double value. So basically public static double (double a) means telling the compiler that okay, let us create a method which is accessible to any class and let it be accessible without creating an object and let it return a double value. And finally, a little correction. That method will need a name so it would be public static double methodName (double a).
31st Aug 2017, 5:38 PM
Amos Aidoo
Amos Aidoo - avatar