+ 2

when to use "public" keyword?

I am very confused on when to use the "public" keyword when declaring a function ie: public void dosomething () vs void dosomething ()

23rd Oct 2016, 7:43 PM
Preston
Preston - avatar
5 Answers
+ 8
use public if you want all classes in any package to have access to that method/attributes. without the word public , the method/attribute is only visible within the current package.
23rd Oct 2016, 7:46 PM
Ousmane Diaw
+ 3
Public is a very common keyword for classes and methods, allowing them to be accessed by the entire application through and by different classes in the program. Private keyword means the data in the class or method is exclusive to that class or method and cannot be accessed or used anywhere else in the application except for the one class or method that it was defined in. Protected keyword is used for superclass/subclass or parent/child relationship created classes. if Class A parent is protected and Class B is child of Class A, they can access the data in those classes. However if Class C has no relation to Class A or B, then the data in A and B is "protected" from being accessed by Class C, or any other classes in the application.
26th Oct 2016, 4:59 AM
Sean Lindholm
Sean Lindholm - avatar
+ 1
public is one of access modifier in Java. In Java, there are three access modifier: private, protected, and public. Here is general concept of access modifier http://javabasictoadvanced.blogspot.co.id/2016/02/oop-concept.html and sample use of access modifier http://javabasictoadvanced.blogspot.co.id/2016/07/access-modifier.html
24th Oct 2016, 12:28 AM
Aula Muttaqin
Aula Muttaqin - avatar
+ 1
public和privateē›øåƹåŗ”ļ¼ŒåÆ¹ę•°ę®ēš„äæęŠ¤äøäø€ę ·ļ¼Œę²”꜉ē‰¹ę®Šč¦ę±‚public即åÆ
24th Oct 2016, 1:47 AM
ę™Øę²é£Ž
ę™Øę²é£Ž - avatar
0
we use public when we want that every class , packages etc can access that class or method.
3rd Feb 2017, 2:43 PM
Ashu Bagul
Ashu Bagul - avatar