What do curly braces after a 'new' statement do?

前端 未结 2 927
无人共我
无人共我 2021-01-01 07:13

I was looking at this example and was wondering what the first line does:

private SiteStreamsListener listener = new SiteStreamsListener() {
<
2条回答
  •  鱼传尺愫
    2021-01-01 07:45

    ArrayList myList = new ArrayList() {
      @Override 
      String toString()
      {
        //
      }
    
      //
    }
    

    Yes you could do that. You can defiantly override public,protected methods. Although you can add new methods but those will not be accessiable through myList instance of ArrayList class.

    Please refer to the java documentation for more details.

    https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#declaring-anonymous-classes

提交回复
热议问题