Public static method vs Static method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Public static method vs Static method

what is the difference when you declare a method like this Public static void name(){ -------------- } or this static void name(){ ------------ }

8th Nov 2020, 8:58 AM
Anaxagoras Anaxagorou
Anaxagoras Anaxagorou - avatar
3 Answers
+ 1
No access modifier means package private. So only classes which are in this package can access this method. With public all classes can access this method which is sometimes not usefull if this method should not be called from outside this package.
8th Nov 2020, 9:07 AM
Jnn
Jnn - avatar
+ 2
Anaxagoras Anaxagorou public static void name(){ } In a simple word. If you use public keyword with a static method that means you are able to access this method from anywhere in your project or program. No restriction at all. //All package, sub packages can access this. You can call this method by using (class_name).(method_name) From anywhere. static void name(){ } // If you are method without any access modifier like publication, private and protected so by default this method will be package private. That means you can access this method only in same package. And you can't access this method outside this package.
8th Nov 2020, 12:47 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
0
thank you guys
16th Nov 2020, 11:26 AM
Anaxagoras Anaxagorou
Anaxagoras Anaxagorou - avatar