The variable name is written in lowercase, and if it consists of more than one word, capitalize the first letter of the other words beginning with the 2nd Word.
If the name of the local variable and the name of the member variable are the same, to use the member variable in the method, you must use the keyword this
class People {
String name = ' Name of class weight definition '; Class and assigns a value
of people () {} public
void Speak () {
String name = "Name defined in the class body method"; Define name and assign
System.out.println (name) with the same name as the member variable in method speak;
System.out.println (this.name); Through this to access the member variables in the class} The public class
testthis { //source file can have only one class that is public, and the name of the source file must be exactly the same as the name of the class, as
//If there is no public class, the name of the source file will be the same as the name of a class. Public
static void Main (string[] args) {
people mypeople = new People ();
Mypeople.speak ();
}
Output results:
the difference between a member variable and a local variable
Member Variable:
1. Member variables are defined in the class and can be accessed throughout the class.
2. The member variable is established with the object's creation and disappears as the object disappears, and exists in the heap memory where the object resides.
3, the member variable has a default initialization value.
Local variables:
1, local variables are defined only in the local scope, such as: functions, statements, etc., only in the area of validity.
2, local variables exist in the stack memory, the scope of the end of the function, the variable space will be automatically released.
3, local variables do not have a default initialization value
The principles to be followed when using variables are: proximity principle
First in the local area to find, there is to use, and then in the member position to find.
The difference between a member variable and a class variable
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 a special description of the object.
The instance variables of the different objects will be allocated different memory spaces, if a member variable in a class has a class variable, then all of the object's class variables are assigned to the same memory, and changing one of the object's class variables affects the other object's class variable, which means the object shares the class variable.
Class Myadd {
int count = 0; Member variable counter
static int sum = 0; Static variable counter
String name;
Myadd (String name) {
this.name = name;
}
public void Myaddmethod () {
count++;
System.out.println (name+ "The value after calling the member variable:" +count);
}
public void Staticaddmethod () {
sum++;
System.out.println (name+ "Call the value of the variable after the class:" +sum);
}
public class Testthis {public
static void Main (string[] args) {
Myadd add1 = new Myadd ("Add1");
Myadd add2 = new Myadd ("Add2");
Add1.myaddmethod ();
Add2.myaddmethod ();
Add1.myaddmethod ();
Add1.staticaddmethod ();
Add2.staticaddmethod ();
Add1.staticaddmethod ();
}
Output results:
The difference between a member variable and a class 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 is loaded, disappearing as the class disappears.
2. Different ways of calling
A member variable can only be invoked by an object.
Static variables can be invoked by objects, or by class names.
3. Different Alias
A member variable is also called an instance variable.
Static variables are also called class variables.
4, the data storage location is different
A member variable is stored in the object of the heap memory, so it is also called the object's unique data.
Static variable data is stored in the static area of the method area (the shared data area), so it is also called the shared data of the object.
The static:★★★ keyword is a modifier that modifies members (member variables and member functions).
Characteristics:
1, want to achieve the object of common data object sharing. This data can be statically decorated.
2. Members that are statically decorated can be invoked directly by the class name. In other words, static members have one more way of calling. Class name. static mode.
3, static with the class loading and loading. and takes precedence over object existence.
Disadvantages:
1, some data is the object-specific data, can not be static modification. Because of that, the unique data becomes the shared data of the object. This is a problem with the description of things. So, when defining static, it is important to be clear whether the data is shared by the object.
2. Static methods can access only static members and not non-static members.
Because a static method is loaded with precedence over the object, there is no way to access the members of the object.
3, the static method can not use the This,super keyword.
This cannot be used because this represents an object, and when the static is there, there may not be an object.
When do you define static members? Or: When you define a member, you don't need to be decorated statically.
Members are divided into two types:
1, member variables. (Static when data is shared)
The data for this member variable is the same for all objects:
If it is, then the variable needs to be decorated statically because it is a shared data.
If not, then say this is the object's unique data to be stored in the object.
2, member functions. (Static is defined when no specific data is invoked in the method)
If you judge whether a member function needs to be decorated statically.
As long as the reference is made, does the function have access to the unique data in the object:
If there is access to the unique data, the method cannot be decorated statically.
If no specific data has been accessed, this method needs to be decorated statically.
The difference between a member variable and a static variable:
1, the member variable belongs to the object. So it's also called an instance variable.
The static variable belongs to the class. So it's also called a class variable.
2, the member variable exists in the heap memory.
Static variables exist in the method area.
3, the member variable exists as the object is created. Disappears as the object is recycled.
Static variables exist as the class is loaded. Disappears as the class disappears.
4, the member variable can only be invoked by the object.
A static variable can be invoked by an object or by a class name.
Therefore, member variables can be called object-specific data, and static variables are called shared data for objects.