Java Basics-The difference between member variables, local variables, and static variables

Source: Internet
Author: User

Before beginning to learn Java, talk about the variables in the Java Foundation (see http://www.linuxidc.com/Linux/2015-02/113862.htm), although know this goods will often use, but did not think of the basic grammar here, There's a pop-up. member variables, local variables, and static variables. Change to change is too easy to let a person dizzy, pick out comb it!

To distinguish the three, first of all, we need to know what they are. Start with the member variable.

member Variables

Let's take a look at one thing:

Property: An external feature, such as the height and weight of a person

Behavior: What can be done, for example, a person has to talk, play ball, etc.

In the Java language, the basic unit is the class, which is used to represent things.

The same goes for describing things with class classes:

Properties: member variables in the corresponding class

Behavior: member functions in the corresponding class

Defining a class is actually a member (member variable and member function) in the definition class

Expansion: The class is an abstract concept, and the object is the concrete existence of the class, embodied. For example: In the life of the car, can be regarded as a class, we call the car class, each car has color and number of tires (can be defined as attributes, i.e. member variables), each car can run (that is, the car's behavior, corresponding to the total member function), we have the car instantiation, will produce an object, such as Mercedes, BMW.

Demo1:

public class Car {
private String color; Define car colors, global variables
private int Numluntai; Define the number of car tires, global variables

Public Car (String color, int numluntai) {
Super ();
This.color = color;
This.numluntai = Numluntai;
}
public void Run () {
System.out.println (this.numluntai+ "wheel" +this.color + "sedan on the Horse Road");
}
}

public class Classtest {
public static void Main (string[] args) {
Car BMW = new Car ("Black", 4); Create a Sedan object, named BMW
Bmw.run ();
}
}

Operation Result:

4 Wheels in a black sedan on the horse road.

where color and Numluntai are called member variables of the car class, this property can be used to describe a class's properties, otherwise it should be defined as a local variable .

For example, I in a For loop is a local variable.

for (int i = 0; i < args.length; i++) {
......
}

Another example is writing in a member method where the variable is a local variable.

Publicclass Car {
private String color; Define car colors, global variables
private int Numluntai; Define the number of car tires, global variables
Public Car (String color, int numluntai) {
Super ();
This.color = color;
This.numluntai = Numluntai;
}
public void Run () {
String carname= "BMW card"; This is the local variable.
System.out.println (this.numluntai+ "+this.color + carname+" sedan on the Horse Road ");
}
}
Publicclass Classtest {
public static void Main (string[] args) {
Car BMW = new Car ("Black", 4); Create a Sedan object, named BMW
Bmw.run ();
}
}

Results:

4 Wheels of black BMW sedan on the horse road

the difference between a member variable and a local variable

Member variables:

The ① member variable is defined in the class and can be accessed throughout the class.

The ② member variable is created as the object is built, disappears as the object disappears, and exists in the heap memory where the object resides.

The ③ member variable has a default initialization value.

Local variables:

① Local variables are defined only within the local scope, such as within a function, within a statement, and only in the area to which they belong.

② local variables exist in the stack memory, the scope of the action ends, and the variable space is automatically freed.

③ local variable has no default initialization value

The principles to be followed when using variables are: proximity principle

First in the local scope of the search, there is the use of, and then in the member position to find.

Static Variables

A variable modified by static is called a static variable, which is essentially a global variable. If a content is shared by all objects, the content should be statically decorated, and the content that is not statically decorated is actually a special description of the object.

Example:

Class person{
Member variables, instance variables
String name;
Static variables, class variables
Properties shared by all objects are decorated with static
static String country = "CN";
public void Show () {
System. OUT.PRINTLN (Country + ":" + name);
Equivalent statement: System.out.println (person.country+ ":" + this.name);
}
}
Class staticdemo{
public static void Main (String[]args) {
Personp = new Person ();
System. Out.println (P.country);
Can be called directly with the class name
System. Out.println (Person.country);
}
}

Results:

CN

CN

the difference between a member variable and a static variable

1, two variables have different life cycles

Member variables exist as objects are created and released as objects are recycled.

Static variables exist as the class loads, disappearing as the class disappears.

2. Different calling methods

Member variables can only be called by an object.

A static variable can be called by an object and can also be called by a class name.

3. Different aliases

Member variables are also known as instance variables.

A static variable is also known as a class variable.

4. Different Data storage locations

Member variables are stored in the object of the heap memory, so they are also called object-specific data.

Static variable data is stored in the static area of the method area (shared data area), so it is also called the shared data of the object.

List comparison:

The difference between a member variable, a local variable, and a static variable

Member variables

Local variables

static variables

Define Location

in a class , outside of a method

Method , or the formal parameter of a method

In a class , outside of a method

Initialize value

There are default initialization values

No , define first , assign value before use

There are default initialization values

Invocation mode

Object invocation

---

Object invocation, class name invocation

Storage location

In the heap

In the stack

Method area

Life cycle

And the object of survival

And the method of survival

Survival with the class

Alias

Instance variable

---

class variables

Summary:

Although the essence of the three are variables, but the use of a considerable difference, a little attention may be caught in the trap. And remember: In a class, if a variable can be used to describe a property of a class, it is defined as a member variable, otherwise it should be defined as a local variable. And if a variable can be used globally (a content is shared by all objects), then we can modify the variable with static, that is, the variable. (also pay attention to its close connection with static methods, which are not described here)

This article permanently updates the link address : http://www.linuxidc.com/Linux/2015-02/113866.htm

Java Basics-The difference between member variables, local variables, and static variables

Related Article

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.