The use of syntax keywords for Java basics

Source: Internet
Author: User

1.super and the use of this

1.1:super represents the parent class object, this represents the current object

The 1.2:super () or this () must be placed in the first row of the construction method, because the system does not find this () or super () call in the first row, it automatically adds super ()

You cannot use the Supe or this adornment in 1.3:static, because the static adornment method belongs to the class, and the caller of the method may be a class, not an object

Use of 2.static

2.1:static can modify inner classes, members, code blocks, methods

2.1.1 Load order: Static properties-Static code blocks-non-static properties-construction methods

2.1.2 Features: Load with class loading, load only once

2.1.3:static cannot be used with the supe or this decoration and cannot be used with abstract

2.2: Internal classes containing static methods must be static internal classes

2.3: A static variable can be a class variable (independent of the object), whereas an instance variable belongs to a specific object.

2.3.1: A static variable is a basic data type, in which case you do not have to create an instance of the class outside of the class to directly use

2.3.2: A static variable is a reference. Because a static variable is a reference to an object, you must initialize the object before you can point the reference to it.

2.4: Static code blocks are primarily used for initialization of classes.

Features: 1. Executes as the class loads;

2. Static code blocks can only be defined within a class and cannot be defined in a method;

3. The variables in the static code block are local variables, only valid within blocks;

4. Static code blocks can only access static members of a class, not access instance members

5. A class can define multiple static blocks of code, executed sequentially.

2.5: Static methods: Unlike class variables (static variables), methods (static methods and instance methods) have only one copy in memory, regardless of how many instances of that class have a common method.

Features: 1. The static method can be used directly, and the instance method must be called by the object after the class instantiation;

2. When calling a static method externally, you can use the "class name. Method Name" or "Object name. Method Name" form. Instance methods can only be used in the following way;

3. Static methods only allow access to static members. The instance method can access static members and instance members;

4.4. This is not used in static methods (because this is associated with an instance);

What is the output of the following code?
 Public classTestextendsbase{Static{System.out.println ("Test Static"); }          PublicTest () {System.out.println ("Test constructor"); }          Public Static voidMain (string[] args) {NewTest (); }} classbase{Static{System.out.println ("Base Static"); }          PublicBase () {System.out.println ("Base constructor"); }}
Staticstaticbase Constructortest constructor
Results
At the beginning of execution, first find the main method, because the main method is the entry of the program, but before executing the main method, the test class must be loaded, and when the test class is loaded, it is found that the test class inherits from the base class, so it will go first to load the base class, When the base class is loaded, a static block is found and a static block is executed. After the base class is loaded, the test class continues to load, and then the static block is executed when the test class is found with a static block. After the required classes have been loaded, the main method is executed. When you execute new Test () in the main method, the constructor of the parent class is called first, and then the constructor for itself is called.
parsing
what is the output of this piece of code? Public classTest { person person=NewPerson ("Test"); Static{System.out.println ("Test Static"); }          PublicTest () {System.out.println ("Test constructor"); }          Public Static voidMain (string[] args) {NewMyClass (); }} classperson{Static{System.out.println ("Person Static"); }     PublicPerson (String str) {System.out.println ("Person" +str); }}  classMyClassextendsTest { person person=NewPerson ("MyClass"); Static{System.out.println ("MyClass Static"); }          PublicMyClass () {System.out.println ("MyClass Constructor"); }} results: TestStaticMyClassStatic PersonStaticPerson testtest Constructorperson Myclassmyclass Constructor parsing: The test class is loaded first, so the static blocks in the test class are executed. Then the new MyClass () is executed, and the MyClass class is not loaded, so the MyClass class needs to be loaded. When loading the MyClass class, it is found that the MyClass class inherits from the test class, but because the test class has already been loaded, only the MyClass class needs to be loaded, then the static blocks in the MyClass class are executed. After loading, the object is generated from the constructor. When generating an object, the member variable of the parent class must be initialized, and the person person in test will be executed=NewPerson (), and the person class has not been loaded, so the person class is loaded first, the static block in the person class is executed, the constructor of the parent class is executed, the parent class is initialized, and then it initializes itself. So the person in the MyClass is then executed.NewPerson (), last execution of MyClass's constructor
View Code

Use of 3.final:

3.1: It can modify non-abstract classes, non-abstract class member methods and variables, and cannot be used with abstract.

3.2: Features:

The 3.2.1:final class cannot be inherited, there are no subclasses, and the methods in the final class are final by default.

The 3.2.2:final method cannot be overridden by the methods of the quilt class, but can be inherited.

The 3.2.3:final member variable represents a constant that can only be assigned once, and the value is no longer changed after the assignment. Final blank, the initial value must be assigned when initializing the object

3.2.4:final cannot be used to modify a construction method.

3.2.5: The private member method of the parent class cannot be overridden by a class method, so the private type method defaults to the final type.

Use of 4.abstract:

4.1: Classes and methods can be decorated, members cannot be decorated

4.2: Features:

4.2.1: cannot be used with static, final, private

4.2.2: Abstract classes do not necessarily contain abstract methods, but classes that contain abstract methods must be declared as abstract classes.

4.2.3: An abstract class can contain a constructor method, but a construction method cannot be declared abstract.

4.3: The difference between an abstract class and an interface:

4.3.1: A class can implement any number of interfaces, but can only be a subclass of an abstract class.

4.3.2: There can be general methods in abstract classes, and interfaces: all methods are abstract.

Modifiers in an abstract class can be public, protected, no modifiers (to indicate that the package is visible), and the interface can be public only, without modifiers

4.3.3: An abstract class can have a construction method, and an interface cannot

4.3.4: The interface must be initialized when declaring a variable

5.public, protect, private difference:

    

Usage of 6.synchronized:

  

    

The use of syntax keywords for Java basics

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.