Use of static in Java

Source: Internet
Author: User

Static statically, as a modifier, was originally introduced by C and initially static represents a local variable that still exists after exiting a block. Then, static represents global variables and functions that cannot be accessed by other files. to C + + and java,static represent variables and functions that belong to the class and are not part of the class object.

From the specific usage point of view, the main use of 5 aspects.

One, static domain.

The static decorated class's Domain (field, member variable), the static decorated domain has only one in the class, it belongs to the class, also known as the class domain. It can only be manipulated by classes and cannot be manipulated by objects. Like what

Pubic class Employee

{

private static int nextid=1;

private int id;

...

}

Here NextID is the static domain, and the ID is the instance domain. NextID only one of the employee classes, all Employee objects share this static domain, even if there is no object, NextID still exists. With instance domain IDs, different objects can have a copy of Id,id belonging to the object, not a class. When operating, use the class to manipulate the static domain, which is employee.nexdid. Manipulate the instance field with an object, that is, Tom.id (Tom is an employee object).

For static domains, static variables are seldom used and static constants are used more often. The more typical is the Pi in mathematics.

public class Math

{

public static final double pi=3.14159265358979323846;

}

This constant is obtained through MATH.PI in the program.

Second, static method

Static methods are statically modified, and static methods belong to classes. The typical or math method, such as POW (x,a), represents a power of x, which is directly written Math.pow (X,a) when called.

A static method cannot manipulate an object or access an instance domain, only the static domain of the class in which it resides. Like what

public static int Getnextid ()

{

Return nextid;//returning static domain

}

Third, static code block

Inside a class, a block of code that is decorated with static, also known as a static initialization block. Primarily used to initialize static domains inside a class. It executes the first time the class is used, only once, and before the constructor. For example, before connecting to a database, you need to initialize the string used to connect to the database and load the driver only once, putting it in a static code block. The sample code is as follows: (pick a part of the code that explains the problem)

Static

{

Need to load the driver's name

Drivername=prop.getproperty ("drivername");

Database address

Dburl=prop.getproperty ("Dburl");

Database user Name

Dbusername=prop.getproperty ("Dbusername");

Database Password

Dbpassword=prop.getproperty ("Dbpassword");

Load driver, driver only needs to load once

Class.forName (drivername);

}

Iv. Static Import

When import imports a class, you can import static and static methods if you add static.

Add such a line of code at the top of the source file:

Import static java.lang.math.*;

We can use the static domain and static method of math directly in the later program.

For example double I=pow (pi,3);

This code is less than the code before the static import

Double B=math.pow (Math.PI, 3);

To be clear a lot, also concise a lot, more easy to understand.

Five, static internal class

The static modifier, which is preceded by the inner class, is the statically internal class. This is used to hide the class from the inside of another class without referencing the perimeter class object. Objects of a static inner class are exactly the same as other inner classes, except that they cannot refer to objects outside the perimeter class.

The above is I read the Java core Technology volume I, combined with learning other content, personal summary of the use of static, there are inappropriate, please correct me.

Use of 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.