+ 1

Help me understand what public static void main does please?

I understand what's shown is public but I don't understand it's concept someone please help me

17th Feb 2018, 4:42 PM
Esther
Esther - avatar
4 odpowiedzi
+ 2
Thank you @Martin, that you corrected me on time!
18th Feb 2018, 1:57 AM
Boris Batinkov
Boris Batinkov - avatar
+ 1
'main' is the name of the method (function), which is the starting (entry) point of the java program. 'public' is access modifier, and means that the method is visible to all classes in the project (you can call it anywhere in the project). - other access modificators are: private and protected. 'static' means that the method is not bound to the instances of the class, but the class itself. 'void' is the return type of the method, and means that the method doesn't return any value, so it cannot be used in expressions. - other return types are: byte, short, int, long, float, double, char, String, Object. They means that the method should return a value of some of the above types. Examples: public static int sum(int a, int b) { return a + b; // We return the sum, which is integer } // Then in my method we call the sum method with two arguments for the parameters a and b. int ourSum = sum(2, 6); Then we can print ourSum, which gets the returned from the method sum integer. System.out.println(ourSum); // Output: 8 // We can use it in expressions int result = sum(2, 6) / 2; // result = 8 / 2 = 4 // If sum was a void type, we have other options public static void sum(int a, int b) { // We just print the result System.out.println("Sum = " + (a + b)); } // Then we can call it in main method, like that: sum(2, 6); // Output: Sum = 8 sum(4, 8); // Output: Sum = 12
18th Feb 2018, 12:55 AM
Boris Batinkov
Boris Batinkov - avatar