Java (basic) classes and variables, java basic variables

Source: Internet
Author: User

Java (basic) classes and variables, java basic variables
Java classes and member variables

When learning programming languages, we need to use them flexibly. How can we flexibly call all function attributes to implement a complete project?

So we need to realize the definition of classes and variables.

1. What is a class?

Class is an abstract concept, and the object is the embodiment of the specific existence of the class. For example, if our class is like a car in our daily life, we can regard it as a class. We call it a small car, each car has a number of tires, colors, and so on (can be defined as an attribute, that is, a member variable), each car can run (that is, the behavior of the car, corresponding to the class of the total member function ), let's take a car as an example to produce an object, such as BMW ).

Column: public class littlecar # defined car class

2. member variables

First look at one thing:

Attributes: appearance features, such as the height, weight, (face value ..... :)

Behavior: what can be done; column: can run, can jump, (can fly ..... I can fly! :)

In Java, classes are the most basic unit, and classes are used to reflect things.

Class is used to describe things:

Attribute: member variables of the corresponding class

Behavior: function of the corresponding member

Defining a class is actually defining the class members (member variables and member functions)

Let's take a look at the next instance:

Column 1:

Public class LittleCar {

Private String color; // defines the vehicle color and global variable.
Private int numLuntai; // defines the number of automobile tires and global variables.
 
Public LittleCar (String color, int numLuntai ){
Super ();
This. color = color;
This. numLuntai = numLuntai;
}
Public void run (){
System. out. println (this. numLuntai + "Wheel" + this. color + "the car is running on the road ");
}
}

 

Public class ClassT {
Public static void main (String [] args ){
LittleCar BMW = new LittleCar ("black", 4); // create a car object named BMW
BMW. run ();
}
}

Running result:

4-wheeled black cars are driving on the road

Here, color and numLuntai are called the Car classMember variablesThis attribute can be used to describe the attributes of a class. Otherwise, it should be definedLocal variable.

3. Local Variables

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

For (int I = 0; I <args. length; I ++ ){
......
}

Column 2:

Publicclass LittleCar {

Private String color; // defines the vehicle color and global variable.
Private int numLuntai; // defines the number of automobile tires and global variables.
Public LittleCar (String color, int numLuntai ){
Super ();
This. color = color;
This. numLuntai = numLuntai;
}
Public void run (){
String carName = "Benz card"; // This is the local variable
System. out. println (this. numLuntai + "Wheel" + this. color + carName + "the car is on the road ");
}
}
Publicclass ClassTest {
Public static void main (String [] args ){
LittleCar BMW = newCar ("black", 4); // create a car object named BMW
BMW. run ();
}
}

 

Result:

Black with 4 wheelsMercedes-Benz brandThe car is driving on the road

4. Differences between member variables and local variables

  

Member variables:

 

① Member variables are defined in the class and can be accessed throughout the class.

 

② Member variables are established with the object and disappear as the object disappears. They exist in the heap memory where the object is located.

 

③ Member variables have default initialization values.

 

Local variables:

 

① Local variables are defined only within the local range, such as within the function and in the statement. They are valid only in the region.

 

② The local variables exist in the stack memory. When the range of the variables ends, the variable space is automatically released.

 

③ No Default initialization value for local variables

 

When using variables, follow the principle of proximity.

First, locate the local range and use it if any. Then, locate the member location.

5. Static variables

Static variables are called static variables, which are essentially global variables. If a content is shared by all objects, the content should be modified statically. Content not modified statically is actually a special description of the object.

Column 3:

Class Person {
// Member variable, instance variable
String name;
// Static variables, class variables
// Attributes shared by all objects are modified using 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 by Class Name
System. out. println (Person. country );
}
}

Result:

CN

CN

6. Differences between member variables and static variables

 

1. different lifecycles of two variables

Member variables exist with the creation of objects and are released with the collection of objects.

Static variables exist with the loading of classes and disappear with the disappearance of classes.

2. Different call Methods

Member variables can only be called by objects.

Static variables can be called by objects or by class names.

3. different aliases

Member variables are also called instance variables.

Static variables are also called class variables.

4. Different data storage locations

Member variables are stored in heap memory objects, 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 objects.

 

7. Comparison of member variables, local variables, and static variables:

Differences between member variables, local variables, and static variables

 

Member variables

Local variable

Static variables

Define location

In the class, outside the Method

Parameters in the method or in the form of a Method

In the class, outside the Method

Initialization value

Has default initialization value

None, defined first, assigned values before use

Has default initialization value

Call Method

Object call

---

Object call, class name call

Storage location

Heap

Stack in progress

Method Area

Lifecycle

Co-survival with objects

Co-survival with methods

Co-survival with category

Alias

Instance variables

---

Class variable

CONCLUSION: (I also learned from my predecessors! So some of them are not very good. Please forgive me! Thank you for your preview. Also, this symbol is very interesting. Haha () The above is a joke! Come on, programmer !!!!)

 

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.