Static in Java

Source: Internet
Author: User

Static indicates the meaning of statics.

The static keyword can be used to decorate a property, to be used as a cosmetic method, and to decorate a class.

(1) Static members (object-independent, class-dependent)

Members use the static modifier----class members (global variables). All objects work together with a unique static member variable. (Constants are typically modified with static)

If a member variable is static, it can be used in the form of a class name. Member variable name. (You can also use the variable name as a member variable name, but use the class name better).
Example: private static final int a = 10; (When instantiating multiple objects, simply divide the static division of the data segment into a storage space of a)

(2) Static methods (typically tool methods for tool classes------methods in Math (class-produced objects have no effect on the consumer, the user uses only their methods))

The static modification method is called a statics method.

Static methods can be accessed by using the class name. Method Name.

Static methods can only inherit and cannot be overridden.

The static method cannot manipulate any properties and behaviors that are not static.



In polymorphic invocation, a static method is a special case where a reference to the parent class is directed to an object of the subclass, and the version in the parent class is still called when the static method is called. Because a subclass cannot overwrite a static method of a parent class. If the same static method is defined in the subclass and the parent class, it is only a hidden effect.


A static method can only access static member variables because non-static members are associated with a particular object, and static methods do not explicitly identify which object.

However, non-static methods can be accessed statically by non-static members.

The This keyword cannot be used in a static method.

(3) Static code block

Static code block:

Static

{

Content

}

The function of static code blocks is generally to do some initialization work.

Static code blocks are executed when the class is loaded. To invoke a class to generate an object, you first need to load the class onto the Java Virtual Machine (JVM) and then load the class with the JVM to generate the object.

Because each class will only be loaded once, the static code block will only be executed once. The construction method is executed when the object is generated, and each build object calls the construction method--------without the static modifier construction method.

If there is a static code block in the inheritance system, then execute the static code block of the topmost class first, execute the static code block to the lowest class, and then execute the constructor of the topmost class, and execute the method to construct the class at the lowest level. Note that the static code is executed only once.

If there are multiple static blocks of code, the JVM executes them sequentially in the order in which they appear in the class.

Add a note: If there are non-static initialization statements, these statements will be executed before the constructor method.


Example: public class Father {
{
System.out.println ("Instantiation of the Parent class");
}
static{
System.out.println ("Parent class static");
}
Public Father () {
SYSTEM.OUT.PRINTLN ("Parent-class construction");
}


}



public class Son extends father{
{
System.out.println ("Instantiation of subclasses");
}
static{
System.out.println ("Sub-class static");
}
Public Son () {
System.out.println ("sub-class construction");
}




public class Test {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Son w = new Son ();
Father f = new Father ();
System.out.println ();
}

}



Operation Result:
Parent class Static
Sub-class static
Instantiation of the parent class
Parent class constructs
Instantiation of subclasses
Sub-class construction
Instantiation of the parent class
Parent class constructs

(4) Static inner class:
A static inner class is a special case of a member's inner class.
You can have the access modifier, the file name of the class file: The outer class class name $ inner class class name. class.
In any method of this class can be instantiated, in a third-party class to be new out, no need to produce external objects, just use "External class class name." The way to access


Static in Java

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.