Java Internal classes

Source: Internet
Author: User
Tags class definition

1) Inner class--a class within an external inner definition.

2) Inner classes can be static or public default protected and private adornments

Note: The inner class is just a compilation concept, and after the compilation is completed, it is two different classes, as in the outside.

3) Classification: member inner class, local inner class, nested inner class, anonymous inner classes

member Inner class: as a member of an external class, you can access the members and methods of the external class, even if it is private.

The external class accesses the members of the inner class and is accessed by the inner class object.

Note: The member inner class cannot contain static variables and methods. because the member inner class needs to create an external class before it can create its own ,

When a member's inner class is referencing an external class object, the Outer.this is used to represent the outer class object;

Instead, you need to create an inner class object that you can use outer.inner obj = outerobj.new inner ();

  1. Public class Outer {
  2. public static void Main (string[] args) {
  3. Outer Outer = new Outer ();
  4. Outer.Inner Inner = Outer. new Inner ();
  5. Inner.print ("outer.new");
  6. Inner = Outer.getinner ();
  7. Inner.print ("Outer.get");
  8. }
  9. //Personal recommendation use GetXXX () to get a member inner class, especially if the constructor of the inner class has no arguments
  10. Public Inner Getinner () {
  11. return new Inner ();
  12. }
  13. public class Inner {
  14. public void print (String str) {
  15. System.out.println (str);
  16. }
  17. }
  18. }

local inner class: refers to the inner class definition within the method and scope. Thinking in Java gives so two examples: defined within a method:

  1. Public class Parcel4 {
  2. Public Destination Destination (String s) {
  3. class Pdestination implements Destination {
  4. private String label;
  5. Private Pdestination (String whereto) {
  6. label = Whereto;
  7. }
  8. Public String Readlabel () {
  9. return label;
  10. }
  11. }
  12. return new Pdestination (s);
  13. }
  14. public static void Main (string[] args) {
  15. Parcel4 p = new Parcel4 ();
  16. Destination d = p.destination ("Tasmania");
  17. }
  18. }
  1. Public class Parcel5 {
  2. private void Internaltracking (boolean b) {
  3. if (b) {
  4. class Trackingslip {
  5. private String ID;
  6. Trackingslip (String s) {
  7. ID = s;
  8. }
  9. String Getslip () {
  10. return ID;
  11. }
  12. }
  13. Trackingslip ts = new Trackingslip ("slip");
  14. String s = ts.getslip ();
  15. }
  16. }
  17. public void Track () {
  18. Internaltracking (true);
  19. }
  20. public static void Main (string[] args) {
  21. Parcel5 p = new Parcel5 ();
  22. P.track ();
  23. }
  24. }

Local inner classes are compiled just like other classes, but only in different scopes, only within the scope of the method or condition, and cannot be referenced after exiting these scopes.

nested INNER classes: nested inner classes, which are inner class that are decorated as static. An inner class declared as static does not require a connection between an inner class object and an external class object, which means that we can refer directly to Outer.Inner, which means that you do not need to create an external class or create an inner class. There is another difference between nested classes and ordinary inner classes: ordinary inner classes cannot have static data and static properties, and cannot contain nested classes, but nested classes can. Nested classes cannot be declared private, and are generally declared public for easy invocation.

Anonymous inner class:

New parent class Constructor (parameter list) | Implement Interface ()
{
The class body part of an anonymous inner class
}

As can be seen from the above definition, an anonymous inner class must inherit a parent class, or implement an interface, but can inherit at most one parent class, or implement an interface.
There are two rules for anonymous inner classes:
1) An anonymous inner class cannot be an abstract class, because an object of an inner class is created immediately when the system creates an anonymous inner class. Anonymous internal classes are therefore not allowed
Defined as an abstract class.
2) anonymous inner classes do not define constructors because anonymous inner classes do not have class names, so constructors cannot be defined, but anonymous inner classes can define instance initialization blocks.

Java Internal classes

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.