Effective Java within class

Source: Internet
Author: User

Effective Java in the requirements of the class, interface, etc., summed up is enough to do, do not give too much access, the definition of the class is also, if a class is only used inside the class, then the class is defined as an inner class bar.

The inner class is divided into four types:

1, static inner class: Static inner class is in front of class more static keyword inner class, this class and class static method and static variable, for the class province to operate, in the static inner class can freely access the static method and static variables of its class.

2, non-static inner class: relative to the static inner class, in fact, everything defined inside the class is limited by the access modifier, so the non-static inner class and the class's Nonstatic method can access everything in its class, except that the meaning of this may change in the inner class.

3. Anonymous class: Anonymous class is a way to define classes when creating class objects, and it's a little difficult to describe them, and here's a simple example.

4, Local class: The local class and non-static inner classes are the same, the difference lies in their scope, the scope of non-static inner class is the entire class, then the scope of the local class is where it is.

Let's give an example of a non-static inner class:

 PackageAlibaba; Public classInnerclass { Public inti; classHelloWorld { Public voidSetI (intk) {i=K; }    }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubInnerclass IC =NewInnerclass (); HelloWorld HW= IC.NewHelloWorld (); Hw.seti (3);    System.out.println (IC.I); }}

The example is simple, the result of printing is 3.

Here's an example of an anonymous class:

Interface PR   {       void  print1 ();   }       Public class Nonameclass    {       public  PR dest ()       {           returnnew  PR () {                publicvoid  print1 ()               {                   System.out.println ("Hello world!!" );               }       };   }   

The last object returned is an anonymous inner class.

But think of the internal class in peacetime to write code in the process of using is not much, just in the source of HashMap to see the non-static internal class entry, may be usually in the process of writing code did not pay attention to it.

Effective Java within class

Related Article

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.