Static,this,extends,super,package in Java

Source: Internet
Author: User
Tags instance method

Static keyword:

Static can be used to modify methods and member variables, and the value of a member variable modified by static is common in all instances of that class

Methods: Methods are divided into member methods (instance methods) and class methods (by static modification, also called static method), you can manipulate static variables and instance variables in instance methods, you can manipulate class variables in class methods

Variables: variables are divided into member variables and local variables (defined in methods, the scope of the statement is small), member variables are divided into class member variables (with static decoration) and instance member variables

This keyword:

1.this is the inner object of the current class, so the rules for accessing class member variables and methods are consistent with the external objects of the class (generally, when new objects are new, the objects are external).
Syntax:this.< variable name > and this.< method name >.
2.this can only be used inside the current class, but cannot be used in static-decorated methods (that is, class methods).
3.this is usually omitted in an instance method, unless the instance method contains a local variable with the same name as the member variable.
Access to member variables requires the use of this.
4. When a class contains more than one constructor method, if one of the constructor methods is to invoke another constructor method, use the
public class this{

Public this () {

This ("white"); Using this method to construct a parameter in an argument-free construction method

}

Public this (String color) {

This.color = color; Assigning a local variable to a member variable through this

System.out.println ("" + color);

}

}

Extends keywords:

Extends inheritance, in subclasses by class< subclass >extends< Parent class > To get the variables and methods already in the parent class, thus improving the inheritance of the Code reusability class

Advantages:
1, improve the reusability of the Code
2, increase the relationship between classes and classes, so that the class presents a different form, which is one of the three main features of Java polymorphism of the performance
Java Inheritance rules:
A Java class can inherit at most one parent class (single inheritance)
But Java supports multiple inheritance
Method overrides are hidden from variables:

To update a method of a subclass on the basis of inheriting the parent class method, override the method:

Same method name, same parameter list, same return value type

To update a variable, you need to change the variable in the child class, and the parent class's variable is hidden
Super Keyword:

To use a method of the parent class in a subclass of a method override, use the keyword super

super.< Method Name >
Sub-parent class construction method features:
1. The first line in the subclass construction method has an implicit statement (Super ())
2. All constructors in a subclass access the parent-class parameterless constructor by default.
When the subclass constructor has a call to other constructors using this (call Super ()), you do not have to construct the method in the reference parent class
the Package keyword:

In order to efficiently manage classes in a project, the concept of package packages is introduced, the class of the same type of function is put into the same package, and the name rule of the package is: the inverted writing of the company's domain names. Name of the project (package name General lowercase)

To refer to a class in a different package, the keyword import is required: Import + package name (the package name can be substituted at a certain level with * instead of java.*)

Import ingestion package often below the package name, note the above location, the package name in the first line of code

Static,this,extends,super,package in Java

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.