Why use method local abstract inner classes

后端 未结 10 1761
名媛妹妹
名媛妹妹 2021-02-05 04:45

One of the legal modifiers you can use with method local inner classes is abstract.

For example:

public class Outer {
    public void method(){
        a         


        
10条回答
  •  无人及你
    2021-02-05 05:09

    You can get the use here http://java-questions.com/InnerClass_interview_questions.html

    which says

    The inner class declared inside the method is called method local inner class. Method local inner class can only be declared as final or abstract. Method local class can only access global variables or method local variables if declared as final

    ie You can declare the static variables in the inner call and use them in the methods.

    EDIT: Why abstract:

    Because if you dont want to create the objects of the inner class. If you create the object in the method then it will be stored in the heap and it is not freed even if the method execution completes as there might be an external reference for this object when it is returned from the method.

    So it depends on whether you want to create an instance or not. If you want to create then use final modifier.

提交回复
热议问题