Java beginners to master-the static Keywords of the basic articles, the entry to master static

Source: Internet
Author: User

Java beginners to master-the static Keywords of the basic articles, the entry to master static

I. Overview
The static keyword declares static variables, which is used by static methods. Static means variables and functions that belong to a class and do not belong to class objects.
Ii. static generation.
When creating an object, unless the new class object is used to create the object, no objects are actually obtained. The data storage space is allocated only when the new class is executed to create the object, the method is for external calls. However, the above methods cannot be solved in two cases.
First, you only need to allocate a single bucket to a specific domain, instead of considering the number of objects to be created, or even creating any objects.
The second is to associate objects of a method (variable) with a class that does not contain it. This method can also be called if no object is created.
To solve the above two problems, the static keyword is born.
When a thing is static, it means that this domain or method will not be associated with any object instance of the class containing it. Even if you have never created any object of a class, you can call its Static method or its static field.

Iii. Example.

TestStatic. java

public class TestStatic {public static int i=33;}

StaticMain. java

public class StaticMain {/** * @param args */public static void main(String[] args) {System.out.println("TestStatic.i="+TestStatic.i);TestStatic s1=new TestStatic();TestStatic s2=new TestStatic();System.out.println("s1.i="+s1.i);System.out.println("s2.i="+s2.i);s2.i=133;System.out.println("s1.i="+s1.i);System.out.println("s2.i="+s2.i);}}


:


Iv. Summary.

In the example, we find that the first printed I value is that people who want a variable not to include its class are associated with objects. This method can also be called if no object is created. The following describes how to allocate a single bucket to a specific domain without considering the number of objects to be created or even creating any objects. The static keyword solves these two problems for us.

Static keywords need to be used with caution to avoid unnecessary troubles for the program.

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.