When creating a method in a class that is to be called from another class, is it necessary to put the public and static keyword? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

When creating a method in a class that is to be called from another class, is it necessary to put the public and static keyword?

JAVA

29th Apr 2017, 12:00 PM
Emmanuel Ngene
Emmanuel Ngene - avatar
4 Answers
+ 1
You must use the public keyword, otherwise you will not be able to call it from another class. Static means that the class is not an instance type.
29th Apr 2017, 12:15 PM
João Rogério
João Rogério - avatar
+ 4
use protected... protected also allows to use method in derived class..
29th Apr 2017, 12:32 PM
Mayur Chaudhari
Mayur Chaudhari - avatar
+ 2
Yes, you can also use protected, if both classes are in the same packadge, otherwise you most use public.
29th Apr 2017, 12:36 PM
João Rogério
João Rogério - avatar
+ 1
I think public is better
29th Apr 2017, 4:27 PM
Emmanuel Ngene
Emmanuel Ngene - avatar