Java-InnerClass-04

Source: Internet
Author: User

If you do not need to associate an internal class object with its peripheral class object, you can declare the internal class as static. It is called a nested class. The nested class is equivalent to an external defined class. This class is rarely used.
A common internal Class Object implicitly stores a reference pointing to the peripheral class object that creates it. However, this is not the case when the internal class is static. Nested classes mean:
1. To create an object of the nested class, you do not need the objects of its peripheral class; 2. You cannot access non-static peripheral class objects from the objects of the nested class;
3. fields and methods of common internal classes can only be placed on the external layer of the class. Therefore, common internal classes cannot have static members or contain Nested classes. however, nested classes can contain all these things.

Why cannot a common internal class have static members?
I hate static!
You 've probably heard about static inner classes. Well, they don't deserve to be called inner classes!
A static inner class (an inner class marked as static) looks like this:
Class outer {
Static class inner {}
'}
I don't like them because they don't give me that special object-to-object bond. in fact, static inner classes aren't even supposed to be called inner classes at all. technically, they are "top-level nested classes ".
A static nested class can be instantiated, but the object created doesn't share any special relationship with an outer object.
The static nested class is tied only to the outer class, not an instance of the outer class.
Outer. Inner I = new outer. Inner ();
That's why you can make an instance of the static nested class without having an instance of the outer class, just the way you can call static methods of a class without having any instances of that class. A top-level nested class is little more than another way to control namespace.
Anyway, a static inner class is not really an inner class
 
I can only guess, and I guess again, even if I guess it, I just think twice. it is useful to have fun only when you do it yourself. so if you don't want to change your tendon (it's not really worth it), remember it.

Static variables cannot be declared in internal classes, which is a protection for Java internal classes.

What are static methods and variables? : They are stored in the static zone during runtime, and other objects can access the static zone. The internal class is intended to hide information, so it is not allowed.

Remember that static content is not internal, so you can understand it. although your top-level program can contain so-called static internal classes, static variables, and static blocks, it is actually something outside the class instance. They do not belong to any class instance.
In a common internal class, a special this reference can be used to link to its peripheral class object. The nested class does not have this special this reference, which makes it similar to a static method.

/** // * Destination. Java :*/
Public interface destination ...{
String readlabel ();
}
/** // * Contents. Java :*/
Public interface contents ...{
Int readvalue ();
}

Class staticparcel ...{
Private Static class staticcontents implements contents ...{
Private int value;
Private staticcontents (INT value)... {This. value = value ;}
Public int readvalue ()... {return value ;}
/** // * The members in the nested class do not have to be static */
}
Private Static class staticdestination implements destination ...{
Private string label;
Private staticdestination (string towhere)... {label = towhere ;}
Public String readlabel ()... {return label ;}
Private Static int distance;
Public static int readdistance ()... {return distance ;}
}
Public static contents getcontents (INT value)... {return New staticcontents (value );}
Public static destination getdestination (string label)... {return New staticdestination (Label );}
}
Class teststaticparcel ...{
Public static void main (string [] ARGs )...{
Contents con = staticparcel. getcontents (11 );
Destination des = staticparcel. getdestination ("Changsha ");
}
}

 

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.