1

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!

5
  • 9
    That is not what static means. Commented Dec 4, 2014 at 0:19
  • static means it's a method of the class, not of an instance of the class, so static methods can't access instance fields/methods directly. public means any code can call it.
    – Bohemian
    Commented Dec 4, 2014 at 0:21
  • static means "belongs to class". This doesn't mean that it can be accessed only by class, but via class (Foo.staticMethod()). public means that you can use it anywhere in your code (in different class, which can be even placed in different package, even it it doesn't extend this class).
    – Pshemo
    Commented Dec 4, 2014 at 0:25
  • 1
    I think you need to study the terms public and static separately.
    – JamesB
    Commented Dec 4, 2014 at 0:28
  • Let's say you are making a custom array class. If we want to merge 2 arrays A and B to make a new array C we can use a static method. But if we want to merge B into A and extend A, we are likely to use a non-static method.
    – Shub
    Commented Jun 14, 2024 at 14:02

4 Answers 4

3

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.

5
  • so if I have a method say public static void do() in a class named Foo, I can call the method in my main method using something like following? Foo foo = new Foo(); foo.do(); while if the do() method is not public, I can only call the method in the class scope? Commented Dec 4, 2014 at 0:23
  • Foo.do() can be called from anywhere. If you call it from within Foo (from a static or non-static method) you can just call do() as it's in scope. You can do this.. Foo foo = new Foo(); foo.do(); but the compiler will actually just call Foo.do() - the instance of foo isn't required. Commented Dec 4, 2014 at 0:28
  • @user3377437 static methods should be called by class name Foo.do(). It is allowed to use references like Foo f = new Foo(); f.do(); but it is discouraged because it creates impression that you are invoking method on f instance, while in reality compiler changes f.do() into Foo.do() automatically, so you could even write Foo f = null; f.do(); and it will work. If you remove public from method modifiers you will end up with default modifier which is also called package-private. This means that you can call do() only from classes from same package as Foo.
    – Pshemo
    Commented Dec 4, 2014 at 0:31
  • 2
    lets not forget do is keyword and can't be used as a method name :) Commented Dec 4, 2014 at 0:36
  • understand! Thank you guys! Commented Dec 4, 2014 at 0:44
2

Others have already explained the right meaning of static.

Can anyone please explain why public static methods are sometimes use?

  • Maybe the most famous example is the 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.

  • Another good examle is a utility class, one that only holds static methods for use of other classes. It dosen't need to be instantiated (sometimes it even can't), but rather supply a bounch of "static" routines to perform, routines that does not depend on a state of an object. The output is a direct function of the input (ofcourse, it might also be subject to other global state from outside). this is actually why it is called 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.

  • That leads to a third common use of public static (or even public static final) - the definition of constants.

  • 2
    • "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" I don't get this part
      – truongnm
      Commented Jun 8, 2022 at 6:48
    • @truongnm I could have probably phrased it better 8 years ago... Let me put it this way: static on a method only means that the method is accessible from the class rather than only from an instance of the class. static on a field means, additionally, that the field does not reside in memory multiple times (per instance), but only once. This meaning is not relevant for methods, since in Java they only reside in memory per class anyway, not per object.
      – Elist
      Commented Jun 9, 2022 at 10:28
    1

    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)

    1

    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?
        }
    }
    
    1
    • You skipped an important example: accessing non-static members from a static code will fail if you don't specify an instance for which you wan't to access the member. You also skipped the nice recursion: public static void main(String[] args) {main(args);};
      – Elist
      Commented Dec 4, 2014 at 0:52

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.