Core Java (7) Static modifier in Java

Source: Internet
Author: User

Static is the meaning of static. It is often used to modify internal classes, methods, and variables.

Static domain

Static modified variables have only one copy in each class. That is, all objects in this class share one variable, which belongs to the class and does not belong to any independent objects.

package com.xujin;class Base{protected static int MAX = 100;}public class Another extends Base{public static void main(String[] args){Base base1 = new Base();Base base2 = new Base();Base base3 = new Base();System.out.println(base1.MAX);//100System.out.println(base2.MAX);//100System.out.println(base3.MAX);//100base1.MAX = 10;System.out.println(base1.MAX);//10System.out.println(base2.MAX);//10System.out.println(base3.MAX);//10}}

Static Constants

Static final modifier, which is a constant.

public class Math{...public static final PI = 3.14159265358979323846;...}

Math. Pi can access this constant.

The public static final modifier is very important. It can be disclosed to other classes but cannot be modified by other classes.

Static Method

The static method is a static method. It is a method that cannot perform operations on objects (that is, there is no implicit parameter This), such as math. random (), math. pow (X, );

You cannot access non-static variables within a static method. You can call this method by Class Name (recommended) or object name (not recommended.

Static methods cannot be overwritten. A non-static (normal) method cannot be rewritten as a static method in the subclass. In addition, a static method cannot be rewritten as a static method in the subclass, because rewriting must comply with polymorphism, and static does not comply with this rule.

Example:

package com.xujin;class Base{public static void aMethod(){System.out.println("Base a");}public void bMethod(){System.out.println("Base b");}}public class Another extends Base{public static void main(String argv[]){Base so = new Another();Another fo = new Another();so.aMethod();//Base afo.aMethod();//Another aso.bMethod();//Another bfo.bMethod();//Another b}public static void aMethod(){System.out.println("Another a");}public void bMethod(){System.out.println("Another b");}}

There is a static method amethod () and a non-static method bmethod (), bmethod () conforms to the polymorphism, while the static method amethod () does not conform to the polymorphism.

Therefore, this is not a method rewrite.

Main Method

When the program is started, there is no object, so the static main method will execute and create the object required by the program.

The main method can also be used to perform unit tests on a class.

Static internal class

Hides a class inside another class. Internal classes cannot reference external class objects and cancel the generated references.

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.