Java Internal classes

Source: Internet
Author: User

Java Internal class summary

Java has a lot of internal class content, not very well understood, but plainly speaking is also a class containing another class.

The basic structure of the inner class

Let's take a look at a piece of code before explaining it.

  1. class Out { //External class
  2. Private int Age = N;
  3. class In { //inner class
  4. Public void Print () {
  5. System. Out. println (age);
  6. }
  7. }
  8. }
  9. Public class Demo {
  10. Public Static void Main (String[] args) {
  11. Out. In in = new out (). New inch ();
  12. inch . Print ();
  13. //or using sowing mode to access
  14. /*
  15. out = new Out ();
  16. Out.in in = Out.new in ();
  17. In.print ();
  18. */
  19. }
  20. }
  21. /* Run Result: 12 */

From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class? This is the only advantage of an inner class because the inner class is free to use the member variables of the outer class, including the private ones, without generating an object of the outer class.

After the program compiles, two. class files are generated, respectively, Out.class and out$in.class, where $ represents the out.inin the above program.

out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note.

    1. The first out is to indicate which outer class the inner class object is to be generated in.
    2. An object of an outer class must be preceded to generate an object of the inner class . Because an inner class can access member variables in an external class, a null pointer exception occurs if the outer class is not instantiated. (But the compiler doesn't allow this to happen.)

PS: C # is not the same as Java, where the inner class of C # cannot access the non-static content of the outer class. C # Inner classes are equivalent to static internal classes of Java

The Access form of variables in the inner class

Access the member variables of the inner class itself directly this . The property name is allowed, and the member variable that accesses the external class needs to use out.this. Property name

  1. class Out {
  2. Private int Age = N;
  3. class In {
  4. Private int Age = ;
  5. Public void Print () {
  6. int Age = +;
  7. System. Out. println ("local variable:" + age);
  8. System. Out. println ("inner class variable:" + this. ) Age);
  9. System. Out. println ("outer class variable:" + out. ) This . Age);
  10. }
  11. }
  12. }
  13. Public class Demo {
  14. Public Static void Main (String[] args) {
  15. Out. In in = new out (). New inch ();
  16. inch . Print ();
  17. }
  18. }
  19. /*
  20. Operation Result:
  21. Local variables: 14
  22. Internal class variable: 13
  23. External class variables: 12
  24. */
Static Inner class
  1. class Out {
  2. private static int Age = N;
  3. Static Class In {
  4. Public void Print () {
  5. System. Out. println (age);
  6. }
  7. }
  8. }
  9. Public class Demo {
  10. Public Static void Main (String[] args) {
  11. Out. In the = new out. inch ();
  12. inch . Print ();
  13. }
  14. }
  15. /* Run Result: 12*/

As you can see , static internal classes can be statically used with static, and the inner classes that are statically built can only access static member variables of the outer class , and have limitations.

Second, because the inner class is statically, the out.in can be viewed as a whole. In short , you can directly new the object of the inner class .

Private Inner class

If you want an inner class to operate only in an external class, you can use private to decorate the inner class.

  1. class Out {
  2. Private int Age = N;
  3. Private Class In {
  4. Public void Print () {
  5. System. Out. println (age);
  6. }
  7. }
  8. Public void Outprint () {
  9. New inch (). Print ();
  10. }
  11. }
  12. Public class Demo {
  13. Public Static void Main (String[] args) {
  14. //This method is invalid
  15. /*
  16. Out.in in = new Out (). New in ();
  17. In.print ();
  18. */
  19. Out = new out ();
  20. Out. Outprint ();
  21. }
  22. }
  23. /* Run Result: 12*/

In the above code, we can only manipulate objects generated by the in class within the out class , and can no longer use out.in in = new Out (). New in () to generate an object of the inner class.

In other words, the inner class at this point can only be controlled by the outer class.

Java Internal classes

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.