Java bizarre syntax-Anonymous inner class

Source: Internet
Author: User

Anonymous inner class what

An anonymous inner class is also a class that is typically created once, used once, without a name, and therefore anonymous.

A lambda expression appears in java8 as if it were for an anonymous class, because sometimes a method is passed a class, only the method of the class is used, and then the method of this class is called, and Lambda can help us to solve the problem easily.

This is the internal class situation, we generally know, but today I met a situation is very interesting, I can record

To a piece of code

/** * @authorRichard* Record this xiaodemo by the way, why do you have a code block inside an anonymous inner class that gets access to the local variable? */ Public classDemo {Private StaticString s =""; Public Static Abstract classparent{Parent(String string) {System. out.println("Nice"+string); }    } Public Static void Main(string[] args) {New Parent("Nice"){//External code block            //s.tostring ();{s.toString();    }        }; }}
Why

The question arises as to why there is a double bracket for {{}} in the anonymous inner class, what is the use of them, first of all, a new Parent () {} This anonymous class enclosing parentheses, which is the code block in our anonymous inner class, where we can write the method, but the innermost {} You should know what it is, and that's our code block, so what can we do with building blocks of code?

Building blocks of code can help me when constructing an anonymous class as a construction code block, you can do something in the construction code block to aid the construction of the inner class

Then the construction code block, and the code block is involved in the domain relationship, the scope is different

In fact, the upper surface method is similar to this class

    //s.toString() 和下面是等价的哈哈    publicstaticclassextends Parent{        //这个位置就是外层代码块        child(String string) {            //这里面就是内层代码块            super(string);            s.toString();        }    }

is not feeling very magical, actually feel also not what meaning, haha, but at that time looked at the feeling is very magical

That is to say, we write the execution statement in the place of the code block, because he is not in the method.

How

Where is the most common place for anonymous classes?
Here to steal a lazy, give a few examples
Recently seen the Jmockit

        newExpectations(){{            try {                departmentService.hasStudent(finalS);                result = Boolean.FALSE;            catch (DepartmentServiceException e) {                e.printStackTrace();            }        }};

Of course, the JDK is more commonly used comparator, such as to the thread pool incoming runnable and so on

Java bizarre syntax-Anonymous inner class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.