Detailed differences between static and non-static internal classes in java

Source: Internet
Author: User
Tags static class

Package static_neibulei_yu_fei_static;

Public class Static_neibulei_Test {

Private static int eye = 2;
Private String name;

Static_neibulei_Test (){

}

Static_neibulei_Test (String name ){
This. name = name;
}

Class Nei {
Private int numNei = 0;
// Private static int numNei2 = 1; // non-static internal class cannot declare static members
Private static final int numNei2 = 1; // macro definition, not a real static member.

Public void funnei (){//

System. out. println (eye); // you can access static and non-static members of an external class.
System. out. println (name); // you can access static and non-static members of an external class.
Fun ();
FunStatic ();
}

}

Static class StaticNei {

// Static internal classes can have static members, but non-static internal classes cannot have static members.
Private int numNeiStatic = 0;
Private static int numNeiStatic2 = 0;

// Non-static members of the static internal class can access static variables of the external class, but cannot access non-static variables of the external class;
StaticNei (){
}

StaticNei (int num ){

This. numNeiStatic = num;

}

Public void funnei (){

System. out. println (eye );
// System. out. println (name); // you cannot access non-static external members.

}

Public static void funneiStatic (){

System. out. println (eye );
// System. out. println (name); // you cannot access non-static external members.

}

}

Public void fun (){
System. out. println (new StaticNei (). numNeiStatic );
System. out. println (StaticNei. numNeiStatic2); // The external non-static method can access static members of the static internal class.
New StaticNei (). funnei ();
StaticNei. funneiStatic ();
}

Public static void funStatic (){

// System. out. println (name); // The static method cannot directly access non-static members.

}

Public static void main (String [] args ){
// Generate a static internal class without external Class Members: this is the difference between the static internal class and the member internal class. Objects of static internal classes can be directly generated:
// Outer. Inner in = new Outer. Inner (); instead of generating external class objects. In this way
// The static internal class becomes a top-level class (normally, you cannot place any code inside the interface, but the nested class can be part of the interface,
// Because it is static. Only place the nested class in the interface namespace, which does not violate the interface rules)

// Create a static internal class object
Static_neibulei_Test.StaticNei staticnei = new StaticNei ();
// Create a non-static internal class object
Nei nei = new Static_neibulei_Test ("external Class"). getNei ();

System. out. println (staticnei. numNeiStatic );
Staticnei. funnei ();
Static_neibulei_Test.StaticNei.funneiStatic ();
Nei. funnei ();
System. out. println (Static_neibulei_Test.Nei.numNei2); // do not use non-static internal classes to access its static members. numNei2 is a macro definition.

}

Private Nei getNei (){
// TODO Auto-generated method stub
Return new Nei ();
}

}

Running result:


Differences between static and non-static internal classes


Package com. devin;

Public class MyMain {
Private static String name = "woobo ";
Private String num = "X001 ";
 
// Static internal classes can be modified using public, protected, and private.
Static class Person {
// Static or non-static members can be defined in the static internal class.
Private String address = "China ";

Private static String x = "";
Public String mail = "kongbowoo@yahoo.com.cn"; // Internal class public Member

Public void display (){
// System. out. println (num); // you cannot directly access non-static members of an external class.

// The static internal class cannot access non-static members of the external class (including non-static variables and non-static methods)
System. out. println (name); // only the static members of the external class can be accessed directly.

// The static internal class can only access static members of the external class (including static variables and static methods)
System. out. println ("Inner" + address); // access the internal class member.
        }
    }

Public void printInfo (){
Person person = new Person ();

// Non-static members of the internal class accessed by the external class: instantiate the internal class.
Person. display ();

// System. out. println (mail); // inaccessible
// System. out. println (address); // inaccessible
System. out. println (person. address); // you can access private members of an internal class.

System. out. println (Person. x); // static members of the internal class accessed by the external class: internal class. Static members
System. out. println (person. mail); // you can access public members of the internal class.
    }

Public static void main (String [] args ){
MyMain staticTest = new MyMain ();
StaticTest. printInfo ();
    }
}

1. Nested class objects do not need their peripheral class objects. That is, it can be instantiated without relying on external class instances.

2. Non-static peripheral class objects cannot be accessed from nested class objects. This is limited by "static methods cannot directly access non-static members" in Java syntax.

3. the members of the external Class access internal class are special and cannot be accessed directly, but they can be accessed through internal class instances. This is because all the members and methods in the static nest are considered static. note that the internal static class Person is only visible within the StaticTest class. If it is referenced or initialized in other classes, it is incorrect.
4. Static internal classes can have static members, but non-static internal classes cannot have static members.
5. Non-static members of the static internal class can access static variables of the external class, but cannot access non-static variables of the external class;

6. Non-static members of non-static internal classes can access non-static variables of external classes.
Generating a static internal class does not require external Class Members: this is the difference between the static internal class and the member internal class. Objects of static internal classes can be directly generated: Outer. Inner in = new Outer. Inner (), instead of generating external class objects. In this way, the static internal class is actually a top-level class (normally, you cannot place any code inside the interface, but the nested class can be part of the interface because it is static. Only place the nested class in the interface namespace, which does not violate the interface rules)
    

Q: differences between static and internal classes in java

A: like a common class, internal classes can also be static. However, compared with non-static internal classes, the difference is that static internal classes do not point to external references. This is actually very similar to the Nested classes in C ++. The biggest difference between Java internal classes and C ++ Nested classes is whether there is a reference pointing to the outside, of course, there are still differences from the design perspective and some details.

In addition, no non-static internal class can have static data, static methods, or another static internal class (internal class nesting can be more than one layer ). However, static internal classes can have all of this.

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.