When should I use the static modification method in Java? What are the advantages or disadvantages?

Source: Internet
Author: User

Static is added when a method or variable needs to be initialized for loading, or is often called.
Static modified method can be called directly with the class name, do not have to instantiate an object before you can call
For example, person has a method in this class public static add () {}
Then you can call Person.add () directly with the person class, or you can use the following method to first make an object in the call can also
If there is no static in front of this method such as public add () {}
Then first person p=new person ();
Then use P.add ();
The class loader has instantiated this class when it loads the class.
Cons: Initialize the load, compare the memory, so do not use the usual method, not recommended to add this keyword.

If Static is written in a single case, high concurrency access is a problem, it is necessary to set the thread to wait, static is loaded in the container when it is loaded into memory, so static methods and variables should not be overused, selective use.

If you need to initialize your static variable with a calculation, you can declare a static block that executes only once when the class is loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:
Demonstrate static Variables,methods,and blocks.
Class Usestatic {
static int a = 3;
static int b;
static void meth (int x) {
System.out.println ("x =" + x);
System.out.println ("a =" + a);
System.out.println ("b =" + B);
}
static {
System.out.println ("Static block initialized.");
b = A * 4;
}
public static void Main (String args[]) {
Meth (42);
}
}
Once the Usestatic class is loaded, all static statements are run. First, the Class property variable begins to be assigned a value, a is set to 3,b by default to 0, then a static block is run, a message is printed, and finally, B is initialized to a*4 or 12. Then call Main (), main () call meth () and pass the value 42 to X. The 3 println () statements refer to two static variables A and B, and the local variable x.
Note: Referencing any instance variable in a static method is illegal.
The following is the output of the program:
Static block initialized.
x = 42
A = 3
b = 12

When should I use the static modification method in Java? What are the advantages or disadvantages?

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.