Java modifier Word static

Source: Internet
Author: User

Static means that statically decorated member variables and member methods are independent of any object of that class. That is, it does not depend on class-specific instances and is shared by all instances of the class. Static means you can use it without instantiating it. The static method is the method without this. A non-static method cannot be called inside a static method, which in turn is possible. It is also possible to invoke the static method only through the class itself, without creating any objects. This is actually the primary use of the static method.    "Although this remark only illustrates the special character of the static method, it is possible to see the basic role of the static keyword, in short, one sentence to describe it: it is convenient to make a call (method/variable) without creating an object. Static variables are also known as static variables, except that the static variables are shared by all objects, only one copy in memory, and are initialized only when the class is first loaded. Instead of static variables, which are owned by an object, are initialized when the object is created, there are multiple copies, and the replicas owned by each object do not affect each other.

Example://Calculate the number of objects produced. If count is not static, each resulting object is initialized.

Package test;

public class Static1 {

public static int count=0;

Public Static1 ()

{

count++;

}

public static void Main (String args[])

{

Static1 s1=new Static1 ();

Static1 s2=new Static1 ();

System.out.println (Static1.count);//can be called directly with the class name.

}

}


This article is from the "11897435" blog, please be sure to keep this source http://11907435.blog.51cto.com/11897435/1856920

Java modifier Word static

Related 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.