+ 2

Why do I need "Public" to call the function? Is there anything else I can use instead

The attached code I need "public" , but idk why https://code.sololearn.com/cGxFYW8AC7L0/?ref=app

7th Feb 2020, 5:42 AM
David Nierman
David Nierman - avatar
10 Answers
+ 2
//If you don't make that entry point public then, Your code will not be executed and you get runtime error, see Access modifiers, method etc
7th Feb 2020, 6:02 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 4
public is an Access Modifier. The Java access modifier public means that all code can access the class, field, constructor or method, regardless of where the accessing code is located. The accessing code can be in a different class and different package...
7th Feb 2020, 6:21 AM
Yaseen Akbari
Yaseen Akbari - avatar
+ 3
Sudarshan Rai 👑 is right. The lessons handle that topic and it begins with methods. Before downvoting please communicate. He is willi g to help.
7th Feb 2020, 5:56 AM
Oma Falk
Oma Falk - avatar
+ 3
The main method must be public because it is called from outside it's own class by the JVM and is executed. Being the entry point of the program, it should be publicly available to be accessed by any other class from any other package.
7th Feb 2020, 6:12 AM
Avinesh
Avinesh - avatar
+ 2
nothing must be public. Even not constructor. But how to create in instance if constructor is not public?
7th Feb 2020, 5:59 AM
Oma Falk
Oma Falk - avatar
+ 2
Additionally there is One entry point at that is main. A Java prog has classes. which are only definitions and computer must know, where ti begin. It is looking for main method.
7th Feb 2020, 6:02 AM
Oma Falk
Oma Falk - avatar
+ 2
Ahh got it, I need a public entry point. Thank you Oma Falk and thank you Sudarshan Rai 👑 sorry about down vote earlier I read the section on methods, but did not see the part on access modifiers. App took me to a specific page instead of whole lesson
7th Feb 2020, 6:06 AM
David Nierman
David Nierman - avatar
+ 1
Oma Falk I read it, but what I don't understand is why does my second function need to be public if nothing needs access to it ? And why won't default work ?
7th Feb 2020, 5:55 AM
David Nierman
David Nierman - avatar
+ 1
So what needs public Access to the second function ?
7th Feb 2020, 5:58 AM
David Nierman
David Nierman - avatar