Variable, constant, static variable, static constant

Source: Internet
Author: User

Variable: string test = "ABC"; // nothing to say

Constant: Use the keyword final in Java to define the constant final string test = "ABC ";

A constant can only be assigned a value once and cannot be modified after being assigned a value.

Static variable: static string test = "ABC"; // class member, which belongs to the class and does not belong to any object. Class is loaded into the memory for the first time.

For example:Class employee {<br/> private string userid; <br/> Private Static string roleid = "12"; <br/>} 

Each object has its own userid, but all instances of this class will share a roleid. If there are 1000 objects, there will be 1000 userids, but there is only one static domain roleid. The static domain roleid exists even if no new object is created.

Static constants: Java uses static final to define static constants.

Example: public static final test = "ABC ";

Static constants can be accessed without a new object, but cannot be modified.

Another static constant used multiple times is system. Out. It declares in the system class:

Public class system {<br/> Public static final printstream out =...; <br/>} 

Therefore, we can directly access the out attribute without a new system object.

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.