Beginners To learn Java, is javase, every day I learn from the day and notes, I hope you can criticize and advice, and promote common progress! Nineth Day!

Source: Internet
Author: User

Day09
    1. Inner class (inner Class): Defines a class within another class, which is called an inner class (built-in class, nested Class)

Features: A. An inner class can directly access members in an external class, including private members. (All members of the external class can be accessed directly because the inner class holds a reference to an inner class, in the format: external class name. This)

B. While the external class is going to access the members of the inner class, you must create an object of the inner class.

Access format: When an inner class is defined on the member location of an external class and is not private, you can create an inner class object directly in the outer other class. Format: outer class name. Internal class Name Variable name = External Class object. Inner Class object

(Outer.Inner oi = new Outer (). New Inner ())

When an inner class is on a member position, it can be decorated with a member modifier. Private, encapsulating inner classes in the outer class. Static, the inner class is statically characterized. An inner class can only access static members of an external class directly, and there is an access limitation.

Note: When a static member is defined in an inner class, the inner class must be static.

When a static method in an external class accesses an inner class, the inner class must also be static.

  1. When describing things, there is something inside the thing, which is described by an inner class. Because internal things are using the content of external things.
  2. Internal classes, such as in methods, cannot be decorated by member modifiers. Members in an external class can be accessed directly because they also hold references in the outer class, but cannot access the variables in the local part where it is located, and can access only the final decorated local variables.
  3. Anonymous inner class: the anonymous inner class is the shorthand format for the inner class. The precondition for defining an anonymous inner class is that an inner class must inherit a class or implement an interface. Anonymous inner class format: New parent class or interface () {Defines the contents of a subclass}. The method name in the inner class. An anonymous inner class is an anonymous subclass object, and this object is a bit more, that is, the object with the content. It is best to define no more than two anonymous inner classes.
  4. Exception (Exception): Occurs when the program is running abnormally. The origin is the embodiment of the object (i.e. encapsulating the problem into an object) that is described by Java in the case of an abnormal situation. In Java, the serious problem of Java through error to reflect, non-serious through the exception to reflect. For error, it is generally not written in the targeted code to deal with it, for the exception can be processed using a targeted approach.
  5. Exception handling: Java provides also some statements to handle, i.e. the code that try{needs to be instrumented} catch (Exception class variable) {The code that handles the exception, that is, the processing method} finally{will execute the statement}.
  6. Common method operations are performed on the caught exception object. String getMessage (); Gets the exception information. E.tostring (); Gets the exception name, exception information. E.printstacktrace (); Gets the exception name, exception information, where the exception occurred. The last method is the Java default exception handling mechanism, which is to call the Printstacktrace () method to print the exception's stack of trace information.
  7. When defining a feature, if there is a possibility of a problem with the feature, it is possible to declare the feature with the throws keyword as an exception. Then other classes or programs call this function, either try,catch or continue to throw (in this case, eventually thrown to the JVM, an exception occurs, handled by the JVM, that is, the exception is reported, and the program stops).
  8. Handling of multiple exceptions: When declaring exceptions, it is recommended to declare more specific exceptions, which can be handled more specifically. The other side declares several exceptions, which correspond to several catch blocks, and do not define redundant catch blocks. If an inheritance relationship occurs in multiple catch blocks, the parent exception catch block is placed at the bottom. It is suggested that in the catch processing, it is necessary to define the specific processing method in the catch, and do not simply define a sentence e.printstacktrack ();

    1. Custom exceptions: In your project, you may have unique problems that are not described by Java and encapsulate objects. So for these unique problems can be in accordance with the Java concept of the problem encapsulation, the unique problem of a custom exception encapsulation. That is, define an exception class and inherit from the exception class. The throw new exception name () is then passed where a specific exception may be thrown, and the corresponding processing action is required.
    2. Custom exception classes must inherit the reason for the exception class: The exception system has a feature because both the exception class and the exception object are thrown. They all have a throwable (unique feature in the system), and only classes and objects in the system can be thrown and throws manipulated.
    3. The difference between throws and throw: throws is used on functions, and throw is used within functions. Throws followed by the exception class, you can follow multiple, separated by commas. The throw is followed by an exception object.
    4. There is a special subclass exception RuntimeException (run-time exception) in exception. If the exception is thrown within a function, the function can be passed without declaration and compilation. If the exception is declared on the function. The caller can pass the compilation without processing. It is not declared on a function because it does not need to be handled by the caller. When the exception occurs, you want the program to stop because, at run time, there is a situation where the operation cannot continue, and you want to stop the program and then fix the code.
    5. When a custom exception occurs, the exception can no longer be evaluated, leaving the custom exception to inherit runtimeexception.

Beginners To learn Java, is javase, every day I learn from the day and notes, I hope you can criticize and advice, and promote common progress! Nineth Day!

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.