A summary of modifiers in Java (except FINA) _java

Source: Internet
Author: User
Tags modifier modifiers

Public/protected/private

Public means open, private for privacy, protected for protection, nothing written for default defaults.

Method:

Public protected Default Private

√√√√ in the same class

√√√x in the same package

In the √√xx subclass

√xxx in different packages

Member Variable:

Consider the security and robustness issues, member variables are generally private, and provide some public methods to modify the member variables, in the modified method of input to judge and control.

Private member variables can only be used inside a class. Public member variables can be used outside of the class.

Class:

The permission modifiers for a class are only two different than public. It does not mean that only the class of the same package can be accessed, and all other classes of the public class are accessible.

Static

is static and is used to modify member variables, methods, blocks of code.

Static member Variable

Static member variables are loaded as the class is loaded, and multiple objects share the static member variable. Look at the following code:

static method

When modifying a method, this method is loaded with the load of the class and is invoked directly by the class name. The methods in many tool classes are static and are invoked directly through the class name. Method Name. Non-static member variables and Non-static methods cannot be used in static methods.

Static code block

A static code block (a Java class, when it produces an object, calls the constructed code block when the constructor method is invoked).

See the following code (proof of the order in which the constructor, construct, and static construct statements are run):

It is to be noted that:

(1) Static methods cannot call Non-static methods, static methods are loaded when the class is constructed, static methods are loaded when the class is loaded, and non-static methods are not loaded until the class is initialized, so static methods cannot invoke Non-static methods. Indirect access is entirely possible by creating an object (or an object passed as a formal parameter) inside a static method.

(2) The main method in Java must be written as static, because the object cannot be created when the class is loaded, and static methods can be invoked without the object. So you can run the program through the main method portal when the class loads.

Note: The parent class is a static method and cannot be overridden in a subclass as a non-static method. A static method in a parent class can be overridden by a static method in a quilt class, but without polymorphism, in a parent-child class, subject to the overriding rule.

When is a class loaded? Timing (deferred loading, can not load without loading

(1) When a new object is loaded

This is well understood and does not explain. (2) No object is created to access static members (methods and properties) in the class

(3) Declaring a reference to a class, not loading (4) creating subclasses, loading the parent class, and then loading the subclass

(5) public static methods in the parent class, subclass inheritance, calling this method using the subclass's class name, loading the parent class

Abstract

This modifier represents an abstraction that can be used in front of methods and classes.

Abstract methods indicate that a class should have this functionality, but different subclasses should have different implementations, so there is no definition of how to do it. Specifically how to do the subclass of their own implementation. Subclasses cannot new objects until all abstract methods are implemented.

If there is an abstract method within the class, then the class must also be abstract. An abstract class cannot direct a new object. Abstract classes can have non-abstract methods.

The above is small series for everyone to bring the summary of the modifier in Java (except FINA) all the content, hope to help everyone, a lot of support cloud Habitat Community ~

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.