+ 8
When to declare a method as static?
Static Method
4 odpowiedzi
+ 4
static members can be directly called without making objects of them
+ 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.
0
class try{
static void test(){
system.out.println("something like this");
}
public static void main(string [] args){
test();
}
}
- 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.