When to Public statci void main method?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When to Public statci void main method??

Hi everyone.. I'm new in learning java. at first sololearn used class Myclass{ public static void main.... then other codes }} but from then input section , sololearn start using import java.util.Scanner before the class Myclass{ .. now I'm confused that where to write the public static void main and why import java.util.Scanner used before everything else? thanks in advance.

25th Dec 2018, 1:28 PM
Tanvir Hassan Kabir
Tanvir Hassan Kabir - avatar
3 Answers
+ 10
Each programming language has an entry point. In case of Java, it is "public static void main" that tells the compiler that this is entry point of the program. If it's not present in the code, then your code won't compile and run. void is the return type of the function, which means this function will return nothing. main is the name of the function. public and static are little bit difficult for you to understand now since you are a new programmer. So when you dive deeper in Java, you'll get to know that what are they and why they are used. They are also told in Sololearn course. But for now remember that public static void main is entry point of your code and you have to write it in your every Java program.
25th Dec 2018, 3:06 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 9
Scanner is a utility provided by the Java to take input either from the user or any other input device. In order to use the scanner, we need to import the scanner utility . And yes it's written on the top of all classes.
25th Dec 2018, 1:52 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
Thanks bro. so the import need to write first . but when exactly to write public static void main(...){ } . I'm very new here. sorry for the messy question.
25th Dec 2018, 2:21 PM
Tanvir Hassan Kabir
Tanvir Hassan Kabir - avatar