Learn Java "3" from scratch--a summary of several important keywords

Source: Internet
Author: User

a summary of several important keywords (a means how to use it?) When does b indicate when to use? C for attention)
(1) Abstract keywords
A. You can modify classes, methods
when modifying a class: The object cannot be instantiated, and must be inherited by the quilt class ;
when modifying a method: There is no method body; It must be written in an abstract class; it must be overridden by a quilt class and declared as an abstract class if the subclass is not rewritten ;
B. Objects that do not exist in the general reality use abstract classes; The method is declared as an abstract method in the parent class when the subclasses are implemented differently .
C. Cannot be used with these keywords: private, static, final, cannot modify the construction method;
(2) Static keyword
A. You can modify member variables, member methods, code blocks, inner classes
==> static variables when modifying member variables: When a class is loaded, a static variable is allocated space in the method area and the initial value is owned by the class, and all objects of the class share a static variable. Access mode: Class name. static variable name
==> static method when modifying a member method: Access Mode: Class name. static method ();
==> Static code blocks when a code block is decorated: executes only once when the class is loaded. ==> static inner class when decorating inner classes: equivalent to static variables, can be decorated with access modifiers, accessed by: external class name. Internal class Name Reference name = new External class name. Internal class name ()
B. Static variables: All objects share a variable. Static method: When a method is not associated with any one member variable, object-independent, static code block: When some external resources need to be loaded, static internal classes can be used to access external static members;
c. Static members cannot access non-static members directly;
(3) Final keyword
A. Can be decorated with classes, member methods, member variables, static variables, local variables;
when modifying a class: The final class cannot be inherited;
Decorated member method: The final member method cannot be overridden by a quilt class;
Modify member Variables: Assign A value to the final member variable in two ways: A, by constructing the method of assignment. B, assigning values directly when declaring variables;
modifying static variables: Assigning values at the same time as declarations, immutable; = = constants; All letters capitalized when named.
Modifying a local variable: the base type is assigned only once, cannot be changed, the "address" referenced by the reference type cannot be changed, and no other object can be referenced. But the content of the referenced object can be changed;
B. When you do not want members to be modified;
The c.final member variable is assigned only once and cannot be changed, and the default constructor cannot assign an initial value to it. The construction method that is displayed is assigned a value to be assigned in all construction methods;
(4) this keyword
A.this is a reference that is stored inside the current object of the heap, referring to the current object;
This can access what content of the object this. Member variable name, this member method name, constructor method in this (parameter);
B. Modify member variables and member methods, you can omit not to write; When a local variable has the same name as a member variable, the Access member variable uses the "this. Member variable" to differentiate between local variables, and other construction methods in the class that are used to access the class: this (parameter) is written in the first row of the constructor method.
(5) Super keyword
A.super is not a reference. Super represents a parent class feature in a subclass object; You can access the member variable of the parent class: Super. member variable; The member method of the parent class: Super. Member method (); You can access the construction method of the parent class: Super (parameter);
B. In a subclass, use the member method in the subclass to access the member variable or member method of the parent class, distinguish member variables and member methods with the same name as the subclass, and in the constructor method of the subclass, call the constructor of the parent class, which must be written in the first row of the child class construction method.
C. If the parent class is called with a constructor method: The call must be displayed in the constructor of the subclass; Super can only invoke the direct parent method;

6. Access permission characters
  (1) class access: public/default, public classes can be accessed in other packages;
(2) Access rights of members

Access rights
Current Class
Current Package
Derived classes
Other Locations
Private
OK
The default OK
OK
Protected
OK
OK
OK
Public
OK
OK
OK
OK



  

Learn Java "3" from scratch--a summary of several important keywords

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.