Can Somebody Explain this java code

前端 未结 5 1184
难免孤独
难免孤独 2020-12-10 08:02

I\'m just starting out on android and my java is verry rusty. I can\'t remember ever seeing a function nested in another function like this before. Could somebody explain to

5条回答
  •  不思量自难忘°
    2020-12-10 08:48

    • This is an Anonymous Class. What is actually happening is that a subclass of Handler is being created with an overridden handleMessage function.

      One of the most elegant things about anonymous classes is that they allow you to define a one-shot class exactly where it is needed. In addition, anonymous classes have a succinct syntax that reduces clutter in your code.

    • You also asked "Could somebody explain to me exactly what final does". A nice explanation can be found here.

      In the case of your example the final keyword stops anybody from being able to assign a new instance / null the instance of the variable "handler" meaning I cannot write the line handler = null; or handler = new Handler() { ... } after your example code snippet.

提交回复
热议问题