0

Please I'm confused; I really cant differentiate between static and public static........ I have a code here to show

when I run this code with "(public) static void" in all the methods I still get the same result........so whats the difference? public class Sololearn { public static void main(String[] args) { add(5,5); sub(20,5); mult(5,4); div(15,3); } static void add(int sum1, int sum2){ int answer = sum1 + sum2; System.out.println(answer); } static void sub(int sum1, int sum2){ int answer = sum1 - sum2; System.out.println(answer); } static void mult(int sum1, int sum2){ int answer = sum1 * sum2; System.out.println(answer); } static void div(int sum1, int sum2){ int answer = sum1 / sum2; System.out.println(answer); } } /* OUTPUT IS: 10 15 20 5 */

2nd Jul 2018, 10:48 PM
Roff Bilex
Roff Bilex - avatar
2 odpowiedzi
+ 4
The fact that you included "public" doesn't change the output of your methods. Public keyword is an access modifier. It only has a meaning /significance if you're dealing with classes. so even if you use "public" or just static, output remains same
2nd Jul 2018, 11:23 PM
Dlite
Dlite - avatar
+ 4
What is "static" ?: static memory: holds variables for start of execution. The "main" function is defined as "static". Other functions are defined as static if necessary. Static variables can be defined in the same way.
2nd Jul 2018, 11:29 PM
BroFar
BroFar - avatar