Java static domain

Source: Internet
Author: User

If you define a domainStatic, Then eachClass has only oneSuch a domain. For comparison, each object has its own copy for all instance domains.

For example, assume that each employee must be assigned a unique ID code. Here we add an instance domain id and a static domain nextId to the Employee class:

class Employee{    ......    private int id;    private static int nextId = 1;}
Now, each employee object has its own id domain, but all instances of this class will share a nextId domain. In other words, if there are 1000 Employee class objects, there are 1000 instance domain IDs. If any instance uses the setId () method, the static fields of the Employee class are changed to the set values. However, there is only one static domain nextId. Even if there is no employee object, the static domain nextId also exists. It belongs to the classAnd does not belong to any independent object. It can also be proved that the static domain is called directly through the class name and the instance domain is called through the (class) instance.

Let's take a look.Static Method:

Static methods cannot perform operations on objects.

For example, Math. pow (x, a) does not use any Math object During computation. In other words, there is no implicit parameter (this ).

It can be considered that the static method does not have the this parameter. (In a non-static method, this parameter indicates the implicit parameter of the method .) For comparison: non-static method call (this.) pow ();

Because static methods cannot operate on objects, they cannot access instance domains in static methods. However, static methods can be accessed.Static fields in its own class.

public static int getNextId(){    return nextId;//return static field}
Call this method using the Class Name: int n = Employee. getNextId ();

Note: The static domain cannot directly access the instance domain (because it is related to the class rather than an object ), however, the instance domain can directly access the static domain (because the instance domain is derived from the class ).

If harry is an Employee object, use harry. getNextId () to replace Employee. getNextId (). However, this method is confusing. BecauseThe calculation result of the getNextId method is irrelevant to harry.. We recommend that you use a class name instead of an object to call static methods.

Use static methods in the following two cases:

1.When a method does not need to access the object state, all the required parameters are provided explicitly (such as Math. pow ).

2.When a method only needs static fields of the category class (for example, Employee. getNextId ).

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.