1. Why the external variables accessed by the inner class need to use the final modification.
Because of life cycle reasons. The local variable in the method, after which the variable will be released, and final guarantee that the variable always points to an object.
First, the inner class and the outer class are in fact at the same level, and the inner class is not destroyed as the method is executed as it is defined in the method. Here's the problem. If the variable in the method of the outer class does not define final, then when the outer class method completes, the local variable is definitely GC, but a method of the inner class has not been executed, and the external variable he refers to cannot be found. If defined as Final,java, this variable is copied into the inner class as a member variable, so that the memory area that the variable points to will not change because the final modified value is always immutable.
2.Java Exception
An exception is an abnormal condition or error that occurs when a program runs (not compiled), and when a program violates a speech rule, the JVM throws an exception that appears. Exceptions are also Java objects that define the base class Java.lang.throwable as the exception parent class. These exception classes include both the error and exception categories.
The error class exception is mainly run-time logic errors, a correct program should not appear in error, when the error general JVM will terminate. Not recommended to capture.
Exception represents a recoverable exception, including checking for exceptions and Run-time exceptions.
Checking for exceptions is the most common exception, such as IO exception sql, which occurs during the compile phase, and is captured by try and catch.
Run-time exceptions, the compiler does not force it to capture and processing, generally will throw the exception up, until the location of the processing code, if no processing block will be thrown to the top, multithreading with Thread.run () thrown, single-threaded with main () thrown.
exception Handling in 3.Java languages includes four links to declaring exceptions, throwing exceptions, catching exceptions, and handling exceptions.
Throw is used to throw exceptions. The throws keyword can declare the exception that the method will throw on the method, and then throw the exception object through throw inside the method.
Try is used to detect whether the wrapped statement block has an exception, and if there are exceptions, throw an exception and execute a catch statement.
CACTH is used to catch exceptions thrown from a try and handle them.
Finally statement blocks are content to be executed regardless of whether there are exceptions.
4.Java Common Tools
Javaprof.exe:Java Profiling Tools;
Jdb.exe:Java Debugger;
Document Builder provided in the Javadoc.exe:JDK
Javac.exe:Java compiler to compile Java source files into bytecode files
Java.exe:Java Interpreter
5.static keyword function.
First, assigning a single storage space to a particular data type or object is independent of the number of objects created, and second, implementing a method or property that is associated with a class rather than an object, that is, you can invoke the method directly or use the properties of the class without creating the object.
Four ways to use static:
(1) static member variable (static variable): Java has no global concept, can achieve the global effect through static modification. A static variable belongs to a class, all instances of which point to the same memory address, and as long as the class in which it resides is loaded, the static variable is allocated storage space.
(2) static member method : A method of a class rather than an object, which can be invoked directly through the class name. The This and Super keywords cannot be used in the static method, and non-static methods cannot be invoked, and only static member variables and member methods of the owning class can be accessed.
(3) Static code block (static code block): In the class is independent, not in any one method body, the JVM in the load class will execute static code block, static code block is often used to initialize static variables.
(4) Static inner class : instantiated without relying on an external class instance object, which needs to be instantiated after an external class instantiation. You cannot have the same name as an external class, you cannot access the normal member variables of an external class, you can only access static members of external classes and static methods.
6.Java The sequence of initialization of the program.
Priority decreases in descending order: (1) static objects take precedence over Non-static objects (static objects are initialized once, non-static objects are initialized more than once), (2) The parent class takes precedence over subclasses, and (3) the sequence defined by member variables.
The order is as follows: The parent class static variable, the parent class static code block, the subclass static variable, the subclass static code block, the parent class non-static variable, the parent class Non-static code block, the subclass constructor.
7. Abstract classes and Interfaces
The
7.1 abstract class typically represents an abstract concept , which provides an inherited starting point and must be used to inherit when designing a new abstract class. Therefore, in a hierarchical structure formed by the inheritance relationship, the leaf nodes should be the concrete classes, and the branch nodes should be abstract classes.
There are a few things to note when working with abstract classes:
(1) An abstract class cannot be instantiated (like an animal just now, instantiation is meaningless), and the instantiation of the work should be done by its subclass, which requires only one reference. The
(2) Abstract method must be overridden by subclasses.
(3) If the class contains an abstract method, the class must be defined as an abstract class, regardless of whether other methods are included. The
(4) Abstract class can contain specific methods, and of course, it can contain no abstract methods. The abstract method in the
(5) subclass cannot have the same name as the abstract method of the parent class.
(6) Abstract cannot modify the same class as final.
(7) Abstract cannot modify the same method with private, static, final, or native.
The
7.2 interface is used to establish a protocol between classes, and it provides only a form, without a concrete implementation. The implementation class that implements the interface at the same time must implement all the methods of that interface. Interface is an extension of the abstract class, Java guarantee data security can not be multiple inheritance, that is, inheritance can only exist a parent class, but the interface is different, a class can implement multiple interfaces, regardless of whether there is no relationship between these interfaces, so the interface makes up for the abstract class can not be multiple inheritance defects However, it is recommended that inheritance and interfaces be used together, as this guarantees both data security and multiple inheritance.
There are several issues to be aware of when using interfaces:
(1) All Method access rights for interface are automatically declared public. The member variable in the
(2) interface automatically becomes the public static final (immutable constant) type. You can name direct access through a class: Implementclass.name. The implemented method does not exist in the
(3) interface.
(4) A Non-abstract class that implements an interface must implement all the methods of that interface. Abstract classes can be implemented without implementation.
(5) You cannot instantiate an interface using the new operator, but you can declare an interface variable that must reference (refer to) An object of the class that implements the interface. You can use instanceof to check whether an object implements a particular interface. For example: if (anobject instanceof comparable) {}.
(6) Be sure to avoid duplication of method names when implementing multiple interfaces.
(7) A class can support multiple interfaces, and multiple classes can support the same interface
7.3 The difference between an abstract class and an interface: the
7.3.1 Syntax Hierarchy
(1) Java interface and Java abstract class have the biggest difference, in that Java abstract classes can provide partial implementations of some methods, and the Java interface can not
(2) The implementation of an abstract class can only be given by subclasses of this abstract class, and any class that implements a method specified by a Java interface can have the type of this interface, and a class can implement as many Java interfaces as possible, so that the class has many types.
(3) in the case of a subclass, it inherits only one abstract class (which is considered by Java for Data security), but it can implement multiple interfaces. The
7.3.2 Design Hierarchy
(1) has different levels of abstraction: Abstract classes are abstract to classes, and interfaces are abstractions of behavior. An abstract class is an abstraction of the whole class as a whole, including attributes, behavior, but an interface that abstracts the local (behavior) of the class.
(2) different design levels: so the abstract class is abstract from the bottom up, the interface is designed from the top down.
(3) different across domains: Abstract classes embody an inheritance relationship, and in order for inheritance to be reasonable, there must be a "is-a" relationship between the parent class and the derived class, that is, the parent class and the derived class should be the same in the conceptual nature. For an interface, it does not require that the implementation of the interface and the interface definition are essentially consistent in concept, but only the contract that implements the interface definition.