Java Learning Note one: variable types

Source: Internet
Author: User

The types of variables supported by the Java language are:

    • Class variable: A variable that is independent of the method and is decorated with static.
    • Instance variable: A variable that is independent of the method, but has no static adornment.
    • Local variable: A variable in a method of a class.
 Public classCount {Private Static intstaticnum=0;//Static Variables        Private intlvars=0;//instance Variable         Public voidmethod () {inti = 0;//Local Variables    }         PublicCount () { for(intI=0;i<20;++i) {//Cycle 20 timesstaticnum++; Lvars++; } System.out.println ("Static:" +staticnum+ "Lvars:" +lvars); }     Public Static voidMain (string[] args) {Count test=NewCount (); Count test2=NewCount (); }}

Execution Result:

static: lvars:20static: lvars:20

Note that static variables are saved in the class, no matter how many instance objects are created, only one staticnum is assigned, plus 20 per creation, and the instance variable is reassigned each time the object is created, and Lvars adds 20 on top of the initial value for each object created.

Java Learning Note one: variable types

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.