When to declare a method as static? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

When to declare a method as static?

Static Method

19th Oct 2016, 7:00 PM
Remmae
Remmae - avatar
4 Answers
+ 4
static members can be directly called without making objects of them
23rd Nov 2016, 7:02 AM
gulshan kumar
gulshan kumar - avatar
+ 3
Static methods are useful when you do not want to instantiate the object to use the functionality in a method. A .NET example of this is the File class. if (File.Exists("file.txt")) { ... } if .Exists(string) was not static you would need to instantiate File and call Exists which interferes with the expected functionality of the File class.
6th Nov 2016, 3:55 PM
Michael Dolence
0
class try{ static void test(){ system.out.println("something like this"); } public static void main(string [] args){ test(); } }
7th Jun 2017, 8:26 PM
Gen. Abdul
Gen. Abdul - avatar
- 3
if you don't want to your child-classes can modify this method, for example if you have some parent-class Daddy with a method say(): class Daddy{ [...] public void say(){ System,out.println("Hello people!") } [...] } and you want to protect tour app againts barking class Child you make say method static.
19th Oct 2016, 8:59 PM
Maciej Góraj
Maciej Góraj - avatar