Getting Started with Java and mastering--The Static keyword of the basic article

Source: Internet
Author: User

I. Overview
The span style= "font-size:18px" >       static keyword is used for static methods that declare static variables. Static means variables and functions that belong to the class and are not part of the class object.
        When creating objects, unless you create objects of that class with new, Otherwise, you don't actually get any objects. The data storage space is allocated only when you execute new to create the object, and the method is called by the outside world. However, there are two cases where the above method cannot be resolved
The first one wants to allocate a single storage space for a particular domain without thinking about how many objects to create, or even creating any objects.
The second kind of person who wants a method (variable) that does not contain its class is associated with the object. That is, you can call this method without creating an object.
        The static keyword was born to solve the two above problems.
        When a thing is static it means that the field or method is not associated with any object instance of the class that contains it. Even if you have never created any objects of a class, you can call its static method or its static domain.

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=" +te STSTATIC.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 printout of the value of I is the realization of the person who wants a variable to not contain its class to be associated with the object. That is, you can call this method without creating an object. The following is an implementation that only wants to allocate a single storage space for a particular domain without thinking about how many objects to create, or even creating any objects. The static keyword solves both of these problems for us.

The static keyword still needs to be used sparingly to avoid unnecessary inconvenience to the program.

Getting Started with Java and mastering--The Static keyword of the basic article

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.