Reputation: 183
If I understand the meaning of each keyword correctly, public
means the method is accessible by anybody(instances of the class, direct call of the method, etc), while static
means that the method can only be accessed inside the class(not even the instances of the class). That said, the public
keyword is no use in this situation as the method can only be used inside the class. I wrote a little program to test it out and I got no errors or warnings without putting the public
key word in front of the method. Can anyone please explain why public static
methods are sometimes use? (e.g. public static void main(String[] args)
)
Thank you in advance!
Upvotes: 1
Views: 3081
Reputation: 5533
Others have already explained the right meaning of static
.
Can anyone please explain why public static methods are sometimes use?
public static void main
method - the standard entry point for java programs.
It is public
because it needs to be called from the outside world.
It is static
because it won't make sanse to start a program from an object instance.
static
.
That said, the static
keyword is not always used because you want to have access to some members in a class without instantiating it, but rather because it makes sense. You keep a property that is shared among all instances in one place, instead of holding copies of it in each instance.
public static
(or even public static final
) - the definition of constants.
Upvotes: 2
Reputation: 2778
A quick guide to some of the options...
public class Foo {
public static void doo() {
}
private static void dont() {
}
public Foo() {
doo(); // this works
dont(); // this works
Foo.doo(); // this works
Foo.dont(); // this works
this.doo(); // this works but is silly - its just Foo.doo();
this.dont(); // this works but is silly - its just Foo.dont();
}
public static void main(String[] args) {
doo(); // this works
dont(); // this works
Foo.doo(); // this works
Foo.dont(); // this works
Foo foo = new Foo();
foo.doo(); // this works but is silly - its just Foo.doo();
}
}
public class Another {
public static void main(String[] args) {
Foo.doo(); // this works
Foo.dont(); // this DOESN'T work. dont is private
doo(); // this DOESN'T work. where is doo()? I cant find it?
}
}
Upvotes: 1
Reputation: 316
A public static method is a method that does not need an instance of the class to run and can be run from anywhere. Typically it is used for some utility function that does not use the member variables of a class and is self contained in its logic.
The code below chooses a path to store an image based on the image file name so that the many images are stored in a tree of small folders.
public static String getImagePathString(String key){
String res = key.substring(3, 4)+File.separator+
key.substring(2, 3)+File.separator+
key.substring(1, 2)+File.separator+
key.substring(0, 1);
return res;
}
It needs no other information (it could do with a safety check on the size of key)
Upvotes: 1
Reputation: 2231
Static methods mean you do not need to instantiate the class to call the method, it does't mean you cannot call it from anywhere you want in the application.
Upvotes: 3