Three types of variables in Java

Source: Internet
Author: User

The three major variables of Java are class variables (static variables), instance variables, and local variables (local variables).

Local variables are local variables that are used in methods, constructors, or blocks, created when a method, constructor, or block is entered, and destroyed once the variable exits.

Local variables have no default values, so local variables must be declared and assigned to them before the first use

Instance variable: Declared in a class, but outside of a method, constructor, or block, the position of each instance variable is created when the object in the heap is allocated a space.

When the object is created with the keyword "new", the instance variable is created, and it is destroyed when the object is destroyed.

The value of an instance variable must be referenced by more than one method, constructor, or block, or an important part of the state of the object that must appear in the class

Instance variables can be used with access descriptors

The instance variable has a default value. Default for number 0,boolean is false, the default value for object reference is NULL

Instance variables can be accessed directly in the class named by name. However, the fully qualified name should be used in static methods and in different classes. Objectreference.variablename

An instance variable is a variable that is attributed to an instance of a class, also known as a member variable, without static modification. Instance variables can only be manipulated by instance of the class name + dot operator + variable.

Class variable: Also called a static variable, declared in a class with the static keyword, but it is outside the method, constructor, or block.

Each class has only one class variable, regardless of how many objects the class has.

In addition to being declared as a constant, class variables are rarely applied. Constants are variables that are declared as public, private, final, and static. The initial value of the instance variable is not changed

Static variables are stored in static memory, rarely using static variables rather than declaring the end or using one of the constants public or private.

Static variables begin and end as the program starts and ends.

Visibility and instance variables are similar. However, most static variables are declared public because they must be used by the consumer of the class.

The default value is similar to an instance variable, except that you can assign a value in a special static initialization area

Static variables can be accessed with the name of the class Classname.variablename

When a static variable is declared as public static final, the variable (constant) name is capitalized. If the static variable is not public and final, its naming method and instance variable are the same as the local variable.

Class variables can not only be manipulated directly by the class name + dot operator + variable name, but also through the class instance + dot operator + variable to operate, in most cases, the former operation mode, one can not effectively use the variable, and secondly can be expressed that the variable is a class variable.

Example:

1 /** 2 *<p>title:java three major variables </p>3 *<p>description: Demonstrating the features of Java's three variables </p>4 *<p>copyright:copyright (c) 2012</p>5 *<p>filename:variabletype.java</p>6  *@authorWang Lu Love7  *@version1.08  */  9  Public classVariabletypeTen {   One  //Defining class Variables A   Public StaticString name = "Wang Lu Love";  -  //Defining instance Variables -   Public CharSex = ' M ';  the    -  /**  - * Method Description: Output the content of blessing to China - * Input parameters: String str + * return type: void -   */   +   Publicstring Print (String str) A{//Defining local Variables atString str1 = "China,";  -      -    returnSTR1 +str;  -  }   -  /**  - * Method Description: Main method in * Input parameters: string[] args - * return type: void to   */   +    -   Public Static voidMain (string[] args) the  {   *SYSTEM.OUT.PRINTLN ("Class variable");  $System.out.println ("Name:" +variabletype.name); Panax Notoginseng      -Variabletype type =NewVariabletype ();  theSYSTEM.OUT.PRINTLN ("Instance variable");  +System.out.println ("Gender:" +type.sex);  ASYSTEM.OUT.PRINTLN ("Local Variables"));  theSystem.out.println ("Blessing:" + type.print ("I love you!") ") ); }   +}

Three types of 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.