+ 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 ()
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.
+ 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.
+ 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
+ 1
publicåprivateēøåƹåŗļ¼åƹę°ę®ēäæę¤äøäøę ·ļ¼ę²”ęē¹ę®č¦ę±publicå³åÆ
0
we use public when we want that every class , packages etc can access that class or method.