Java Object-oriented _static keywords

Source: Internet
Author: User

1. Modifier member Variable: Static variable with the statics modifier, and no static modifier called instance variable.

Instance variable: An object that belongs to an object that has one copy. When an object is created, it is initialized, there are multiple replicas, and the replicas owned by each object do not affect each other. stored in the heap.

Static variables: Static variables are shared by all objects and have only one copy in memory, which is initialized only when the class is first loaded.

Demo:

1  Public classSpike2 {3      Public Static voidMain (string[] args)4     {5Counter A =NewCounter ();6 System.out.println (A.increment ()); Output 07 System.out.println (A.anotherincrement ()); Output 28Counter B =NewCounter ();9 System.out.println (B.increment ()); //Output 2 Ten     } One } A classCounter - { -     Private Static intCount = 0; the      Public intIncrement () -     { -         returncount++; return count and execute + + statement first -     } +      Public intanotherincrement () -     { +         return++count; A     } at}

Java Object-oriented _static keywords

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.