1

I am new to Java. I am reading a toy code, and notice all the other methods expect "start" and "stop" methods are static. And due to this "stop" can only be called as via ".this.stop()" (The "here" line). What's the advantage implement like this, why not make "start" and "stop" also static methods?

public class MyService {
    private MyService() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    MyService.this.stop();                    <----- here
                } catch (IOException | InterruptedException e) {
                    ...
                }
            }
        });
    }

    protected void stop() {
     ....
    }

    protected void start() {
     ....
    }

    public static xxx getXXX() {
        return xxx;
    }
    ....
}
user1269298
  • 717
  • 2
  • 8
  • 26
  • 2
    A static method belongs to a class, rather than an instance of it. It seems that the purpose of the start and stop methods are to start or stop an instance of a class and not the class itself. A static method can be called by the class as a whole (e.g MyClass.staticMethod()) and you don't need an object to call it. – Nora Na Nov 05 '21 at 06:29
  • Does this answer your question? [What is the difference between a static method and a non-static method?](https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method) – Progman Nov 06 '21 at 12:02

2 Answers2

7

The problem is that the stop() call is inside an anonymous class extending Thread so it has its own stop() method inherited from Thread:

   new Thread() {
        @Override
        public void run() {
            try {
                MyService.this.stop();                    <----- here
            } catch (IOException | InterruptedException e) {
                ...
            }
        }
    }

The whole statement is MyService.this.stop();, it is not just this.stop() which would be no difference to stop() alone in this case. This notation - MyService.this - is used inside enclosed (nested) classes to denote the enclosing instance, the instance of MyService in this case. See Java Language Specification 15.8.4. Qualified this.

This notation is only needed if the enclosed class has a member with the same name/signature as the enclosing class. In this case, if the method had a different name, stopService for example, there would be no need for a qualified or simple this at all.


Note: since it is not intended to change the functionality of the Thread class, I recommend not to extend it, but instead to use a Runnable passed as argument in its constructor:

    new Thread(new Runnable() {
        @Override
        public void run() {
            // the code
        }
    });

This would avoid having to use the qualified this (it still can be used).
It would also avoid any unwanted access to the Thread members.

user16320675
  • 135
  • 1
  • 3
  • 9
0

this means this instance (in the scope). So, if the stop or the start method is meant to stop or to start a MyService instance, it should be non-static.

hata
  • 11,633
  • 6
  • 46
  • 69