Variables in Java

Source: Internet
Author: User

Variable

Variables in Java programs can be broadly divided into member variables and local variables .

Local variables

  formal parameter : a local variable defined in a method signature, which is the responsibility of the method caller to assign a value to it, which dies with the end of the method.

  local variables within a method: Local variables defined within a method must be explicitly initialized within a method. This type of local variable takes effect from the completion of initialization and dies with the end of the method.

  Local variables within code blocks : Local variables defined within a block of code that must be explicitly initialized within a block of code. This type of local variable takes effect from the completion of initialization and dies with the end of the block of code.

  Local variables are short-lived, and they are stored in the stack memory of the method.

Member variables

  A variable defined in a class is called a member variable (English is field).

If the member variable is defined without a static decoration, the member variable is also known as a non-static variable or an instance variable , and if a static decoration is used, the member variable can also be called a static variable or a class variable. (The initialization time of a class variable is always before the initialization of the instance variable)

static can only decorate member parts that are defined in a class, including member variables, methods, inner classes, initialization blocks, and internal enumeration classes.

 Public classVariable {//member Variable age    intAge ; //class variable name    StaticString name; //str Parameters     Public voidShow (String str) {//local variables in the method        intMethod = 0; Static inttest=123;//here, error. Static local variableSystem.out.println (str+method); }        {//code blockString Codeblock= "code block"; }         Public Static voidMain (string[] args) {Static inttest=123;//here, error. Static local variable    }}

Variables in Java

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.