Packages and inner classes in Java

Source: Internet
Author: User

Packages: Package Cn.itcatst.chinamobile--------CN. Company domain name. Module name
The declaration of packages in Java is to use the package and ends with a semicolon and must also be placed in the first line of the source code
Automatically create a package at compile time: javac-d. Class name. java (. = current directory)
This is required at run time: the Java package name. Class Name (the full name of the class is: Package names. Class name)
Learn: Common packages in Java
1. Java.lang contains some of the core classes of the Java language, such as String, Math, Integer, System, and thread, providing common functionality that does not have to be imported manually
2. java.awt contains several classes that make up the abstract window toolset (toolkits), which are used to build and manage the graphical user interface (GUI) of the application.
3. java.net contains classes that perform network-related operations
4. java.io contains classes that can provide multiple input/output functions
5. Java.util contains some utility classes, such as defining system features, using classes related to date calendars
Guide package: Import java.util.*; Import all the classes in the Java.util package (if you want to declare the package--the next line of the package is to be placed)
The access modifier---public/default/protected/private abstract/final/static
Used in class: public/default/abstract/final
public/default/protected/privateabstract/final/static used in member methods
public/default/protected/privatefinal/static for member variables
public/default/protected/private used in the construction method
Summary: The class is preceded by public, the properties in the class are private, the methods are public, the parent class is generally abstract, the functional method in the parent class is generally used abstract
Internal classes (Inner Class)
The inner class is a standalone class that, after compilation, is compiled into a separate. class file, but preceded by the name of the outer class and the $ symbol;
Internal classes are divided into: Member inner class, Static inner class, local inner class, Anonymous inner class
member Inner class: In other places to access members of an inner class you must create an object of the inner class
Outer class Outer object = new External Class (construction method); Inner class Inner object = outer object. New Inner Class (construction method);
If the inner class is private, access to the inner class object can be achieved through getter methods, which does not break the encapsulation
Static inner classes: There are two scenarios for accessing static inner classes in an external class:
The first is to access a non-static member outer class in a static inner class in an external class. Inner class Inner class object = new Outer class. Inner Class (construction method);
The second type: accessing static members in a static inner class in an external class can be either through the previous or direct external class name. Internal class name. Member
Non-static External class object cannot be accessed from an object in an inner class
Local Inner class: location: The class defined in the method
A local inner class can only be instantiated within a method that defines the inner class, and it cannot be instantiated outside of this method.
An object of a local inner class cannot use the non-final local variable of the method in which the inner class is located-----Explain the existence life cycle length
Anonymous inner class: There is no name of the inner class-------formally divided into: 1, the Inheritance of anonymous inner class; 2, the anonymous internal class of the interface type
Premise: An anonymous inner class must inherit a parent class or implement an interface, and can only be one;
Syntax: The new Interface () {} is equivalent to implementing this interface, and an anonymous implementation
The new parent class () {} is equivalent to inheriting the parent class, and anonymous inheritance is primarily for anonymous polymorphism
Anonymous inner class Usage principles: 1, Anonymous inner classes cannot have a constructor method; 2. Anonymous inner classes cannot define any static members, methods.













Packages and inner classes in Java

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.