In this code why the "public" is place in front of "class" eventhough the "public" is placed inside the "class" para thesis.{} | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

In this code why the "public" is place in front of "class" eventhough the "public" is placed inside the "class" para thesis.{}

https://code.sololearn.com/cCOWyWlPBQeT/?ref=app

11th May 2020, 9:50 AM
Yogeshwaran
4 Respostas
+ 1
That public before "class" doesn't make a difference here because you're writing all code in one file here. When you will be making code in different files you have to put your public classes in separate file because that is accessible from anywhere, but when you are making a package or something and you want class to be not accessed from outside class/methods you have to make it private. And the main method of any class should always be public otherwise you can't run the code.
11th May 2020, 9:53 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
The first public is for the class, so defines the maximum access level for everything inside. The second public is for a method. This means that the method is accessible from outside. When a class is public but a method is not external sources cannot access it. That is also true when the class is Not public, but the method is.
11th May 2020, 9:56 AM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 1
Thanks šŸ”« Rick Grimes and Manu_1-9-8-5 now I understand why the public is placed before the classšŸ˜ŠšŸ˜Š
11th May 2020, 10:00 AM
Yogeshwaran
0
Whenever you create a file in either Netbeans, Eclipse or any other IDE, always remember that a single file can have multiple classes within it but there can only be 'one public class' and the file name should be named after it. If the class containing the main method is not public then the JVM will not be able to locate it and you will get an error. Also the main method should also be public for the same reason. Sololearn works differently, so do not get confused with it.
11th May 2020, 11:40 AM
Avinesh
Avinesh - avatar