Use of Java internal classes!

Source: Internet
Author: User

I did not study Java as a junior college student, so I am not very good at some concepts or a bit vague about some of the things that are not commonly used. I just want to learn some basic things recently. Today I am mainly familiar with the use of internal classes!

1. Overview
An internal class refers to defining another class within an external class.
An internal class is a member of an external class and is attached to an external class.
Internal classes can be static and can be modified using protected and private. (The external class cannot: The External class can only

Use public and default ).

2. Category
Member internal class,
Local internal class,
Static internal class,
Anonymous internal class (graphics are required and must be mastered ).

Advantages and disadvantages of internal classes:

Advantage: reduces the size of bytecode files generated after class file compilation.
Disadvantage: unclear program structure

What are the advantages of Java internal classes? Why Internal classes?

First, let's take a simple example. If you want to implement an interface, but a method in this interface has the same name and parameter as a method in the class you have conceived, what should you do? In this case, you can create an internal class to implement this interface. Since all the content of the internal class is accessible to external departments, this can complete all the functions that you can directly implement this interface.

But you may have to question it. Isn't it enough to change the method?

Indeed, it is unconvincing to use this as a reason to design internal classes.

The real reason is that the internal classes and interfaces in Java can be combined to solve a problem that is often complained by C ++ programmers that there is not much inheritance in Java. In fact, the multi-inheritance design of c ++ is very complicated, and Java can implement multi-inheritance effectively by adding interfaces to internal classes.

Public class innercls {Public String STR = "This A not static property! "; Public static string stastr =" this a static property ";/** static inner class */static class stainner {public static void dosomthing () {system. out. println ("static method>" + stastr); system. out. println ("*********** line ******************");} public void testsomthing () {system. out. println ("no static method>" + stastr) ;}/ ** not static inner cannot have a static method */class inner {public void dosomething () {system. out. println ("#######################"); system. out. println (STR); system. out. println (stastr) ;}} public static void main (string [] ARGs) {/** for static inner class */innercls. stainner. dosomthing (); innercls. stainner is = new innercls. stainner (); is. testsomthing ();/** for not static inner class */innercls ICS = new innercls (); Inner II = ICs. new inner (); II. dosomething ();}}

Package COM. lean. innercls. used; public class outerfincls {public static void main (string [] ARGs) {outer = new outer (); outer. makeinner () ;}} class Stu {public void print () {system. out. println ("This Is A Stu") ;}} class course {public void print () {system. out. println ("this is a course ");}} /** you can also use internal classes to implement multiple inheritance or methods with the same name */class Outer extends course {private string outername = "fkinn "; class inner extends Stu {} public void makeinner () {inner = new inner (); inner. print ();}}

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.