Java Internal classes

Source: Internet
Author: User

1. member internal class Member Inner class

Using an inner class within a class, you can directly access any member variables and methods of its class in the inner class.

Create an instance of the member's inner class inside the outer class:

This.new innerclass (); or new Innerclass ();

Create an instance of an inner class outside the outer class:

(New Outerclass ()). New Innerclass ();

To access members of an external class in an inner class:

Outerclass.this.member

2. Static internal class static Inner class

Static inner classes can only use static members and methods of external classes, not non-static members and methods.

Create an instance of the member's inner class inside the outer class:

New Innerclass ();

Create an instance of an inner class outside the outer class:

New Outerclass. Innerclass ();

3. Local internal classes local Inner class

Local inner classes in the method, like local variables, can not have public ,protected,private, such as keyword modification, it can not access the external class member variables and methods, only access to the final variable inside the method .

4. Anonymous inner class Anonymous Inner class

The anonymous inner class is a local inner class without a name, not using the keyword class, extends, implements, and no constructor method.

An anonymous inner class implicitly inherits a parent class or implements an interface.

Anonymous inner classes are used more often as a method parameter.

Outerclass:

Package Javase.innerclass;

Public class outerclass {

Static int x=0;

int y=0;

Public class memberinnerclass{

Private int y=0;

Memberinnerclass () {

y=5;

}

Public void Fun () {

System. out . println ("fun function from member inner class");

//Call the method in the inner class if the inner class does not exist for this method.

//While an external class exists, the method in the external class is called.

Printy (10);

//Call a method in an external class

Outerclass. This . Printy ();

}

Public void printy (int y) {

System. out . println ("printy function from member inner class");

//output passed in the parameter y

System. out . println ("function parameter y="+y);

//Output attribute y in the inner class

System. out . println ("attribute y= in inner class "+this. ) Y);

//Output attribute y in the external class

System. out . println ("attribute y= in an external class "+outerclass. this. Y);

}

}

Public void Printy () {

System. out . println ("Printy function from Outter class");

//Create an instance of a new member's inner class inside an external class, or you can write:

//memberinnerclass innerclass=new memberinnerclass ();

Memberinnerclass innerclass=this. New Memberinnerclass ();

//Create a new instance of the static inner class inside the outer class

Staticinnerclass staticclass=New staticinnerclass ();

//Member variable in output member inner class

System. out . println ("Member intrinsic class attribute y="+innerclass. Y);

//Output member variable in external

System. out . println ("External class attribute y="+y);

//Output a member variable in a static inner class

System. out . println ("Static intrinsic class attribute x="+staticclass. x);

}

Public static class staticinnerclass{

Private int x=10;

Public void Printx () {

System. out . println ("Printx function from static inner class");

//Output the variables in the inner class

System. out . println ("Static intrinsic class attribute x="+x);

//Only static members and methods of external classes can be called

System. out . println ("External class attribute x="+outerclass. x);

}

}

Public void Testlocalinnerclass () {

Final int k=1;

//local inner class

class localinnerclass{

Public void PrintK () {

System. out . println ("PrintK function from local inner class");

//Only the final variable in the method can be accessed

System. out . println ("Variable k= in the method in which the local inner class resides "+k);

}

}

Localinnerclass localclass=New localinnerclass ();

LOCALCLASS.PRINTK ();

}

}

Testouterclass:

Package Javase.innerclass;

Import java.util.Date;

Public class testouterclass {

Public static void main (String args[]) {

Outerclass out=New outerclass ();

System. out . println ("===== member internal class ==========");

//Instantiate a local inner class object outside of a class

Outerclass.memberinnerclass in= out. New Memberinnerclass ();

In.fun ();

System. out . println ("===== Static internal class ==========");

//Instantiate a static inner class object outside of a class

Outerclass.staticinnerclass staticinnerclass=New outerclass.staticinnerclass ();

Staticinnerclass.printx ();

System. out . println ("===== local internal class ==========");

//local inner class

Out.testlocalinnerclass ();

System. out . println ("===== Anonymous internal class ==========");

//Anonymous inner class , equivalent to inheriting the date class, and overriding the tostring method

System. out . println (new Date () {

Private static final long serialversionuid = 1L;

@Override

Public String toString () {

return "Hello"+Super. toString ();

}

}.tostring ());

}

}

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.