"Java" Variable class interface _ learning Notes

Source: Internet
Author: User
Tags class definition constant definition

variables, classes, and interfaces

1 , the type of the variable

Instance variable (not modified with static)

Member variables

Class variable (with static decoration)

All variables

Formal parameters (variables defined in the method signature)

Local Variables method local variables (defined within a method)

code block local variables (defined within a code block)

2 , Singleton class ( Singleton )

Singleton class: A class can only create one object, called a singleton class.

Usage:

A, use the private modifier constructor.

B, use the public static class name return type getinstance (), create the object in the method, and return the object.

3 , Final modifier

A, final modified member variable: You cannot assign A value after you have obtained the initial value.

A, class variable: You must specify an initial value in a static initialization block or declare the variable when it is declared, and can only be specified in one of two places.

b, Instance variable: The initial value must be specified in a non-static initialization block, a declaration instance variable, or a constructor, and only one of the three places can be specified.

B, final modifies local variables: The system does not initialize local variables, you can specify either a default value or not.

C, final decorated base type variables and reference type variables:

A, final modifies a primitive type variable when the value cannot be re-assigned.

B. When the final modifier refers to a variable, the address of the referenced object cannot be changed, but the object's contents can be changed.

D, Final modified method: The final modified method cannot be overridden.

E, Final Modified class: Cannot be inherited.

4 , abstract methods, and abstract classes

Abstract method:

A, decorated with the abstract modifier.

B, cannot have method body.

Abstract class:

A, use the abstract modifier.

B, cannot be instantiated.

C, can contain member variables, methods including ordinary methods, constructors, initialization blocks, inner classes (interfaces, pieces) 5 components, the constructor is mainly used for subclass calls.

D, abstract classes can have no abstract method, but the class containing the abstract method (inheriting the abstract parent class does not fully implement the parent abstract method, implements an interface, but does not fully implement the interface abstract method) must be defined as an abstract class.

7 , Interface

( 1 ) Usage:

Modifier interface Interface name extends parent interface 1, parent interface 2 ...

{

Constant definition;

abstract method definition;

internal class definition;

Interface definition;

Enumeration class definitions;

Default method or class method definition (JAVA8 is supported)

}

A, the interface modifier is public or omitted (omitting the default use of the package modifier).

B, an interface can have more than one parent interface, but cannot inherit a class.

C, the interface can not have constructors and initialization blocks.

D, member variables in the interface (static constants), methods (abstract methods, class methods, default methods (JAVA8)), inner classes (including internal interfaces, enumerations).

E, the member in the interface is the public permission, if there is no modifier, the default is the public adornment.

F, static constants in the interface must be modified with static final, no, the default is the static final decoration.

G, in the interface is not a class method and the default method of the ordinary method, there should be an abstract decoration, no, the default is the abstract decoration.

H, interfaces cannot be instantiated, but reference types can be declared.

( 2 ) Using the interface:

Modifier class class name extends parent class implements interface 1, interface 2 ...

{

Class Body

}

A, implement the interface, you can get the interface of the member variables (static constants), methods (abstract methods and default methods)

B, when a class implements an interface, all of the abstract methods must be fully implemented, otherwise it will be inherited to an abstract method, and the class must also be defined as an abstract class.

C, interfaces cannot show inheritance for any class, but reference variables of interface types can be assigned to reference variables of type object.

8 , interfaces, and abstract classes

Interfaces: Abstract methods, Java8 default methods and class methods, cannot define static methods

A, method

Abstract classes: Abstract methods and common methods, you can define static methods

Interface: Only static constants can be defined

C, member variables

Abstract classes: You can define static constants, or you can define normal member variables

Interface: no constructor

D, constructor

Abstract class: There is a constructor, but cannot be instantiated, for the initialization of a subclass call

Interface: no initialization block

E, initialization block

Abstract class: can have initialization block

Interface: A class can implement multiple interfaces

F, inheritance implementation

Abstract class: A class can inherit only one abstract class

9 , Inner class

A, the inner class can directly access the private data of the external class.

B, the external class cannot access the implementation details of the inner class.

C, the inner class can use the private, protected, static three modifier, the external class cannot, because the outer class is a package, so it has only two scopes-within the same package or anywhere, so it has only the public modifier and the default modifier.

D, non-static inner classes cannot have static members.

( 1 ) non-static inner class

A, defined within a class, the inner scope is anywhere in the class, even inside the method.

B, the inner class is generally defined as a member's inner class, as a class member that is similar to member variables, methods, etc.

Local inner classes and anonymous inner classes are not class members.

C, non-static inner classes can access external private members, which in turn cannot.

D, the inner class object is parasitic in the Outer class object, but the outer class object does not necessarily contain the inner class object.

E, the static members of the outer class are not allowed to use members of the inner class directly.

F, non-static inner classes are not allowed to define static members.

( 2 ) static inner class

A, using static to decorate the inner class, the inner class belongs to the outer class itself, called the class inner class or static inner class.

B, static inner classes can contain both static and non-static members.

C, static inner classes can only access static members of external classes.

D, external classes cannot directly access static inner class members.

An inner class can be defined in an interface, but it must be a static inner class, which is modified with public static.

Ten , using the inner class

A. Define an inner class outside the outside class

Outerclass.innerclass VarName

B. Create a non-static inner class instance outside the outer class

Outerinstance.new Innerconstructor ()

One , anonymous inner class

New Implementation Interface () | Parent class Constructor (argument list)

{

Anonymous inner class class body

}

A, anonymous inner class must implement an interface or inherit a parent class.

B. An anonymous inner class cannot be an abstract class, because an inner class object is created immediately.

C, an anonymous inner class cannot define a constructor, because it does not have a class name and cannot be defined.

D, anonymous inner classes can define initialization blocks.

Before E, Java8, local variables that were accessed by local inner class access and anonymous inner classes must be final decorated.

"Java" Variable class interface _ learning Notes

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.