This: This indicates the current object (class)
--Use the This keyword in the method definition of a class to represent a reference to an object that uses the method
--Use this when you must indicate who is currently using the method's object
--Sometimes this can handle the case of member variables and parameter names in a method
--this can be seen as a variable whose value is a reference to the current object
public class ball { // Member variable (property) private String name; private int weight; // method of constructing class public Ball (String name, int Weight) { this . Name = name; this . Weight = weight; }
The member variable is the same as the name of the formal parameter in the ball () method, which is name weight. Using the This keyword
THIS.NAME Specifies the name member variable in the Boll class, and the second name in the THIS.NAME = Name statement specifies the formal parameter. The function of the ball method essentially is to assign the value of the parameter name weight to the member variable name weight
static: Not inside the heap nor inside the stack, in the data seg
--Class name directly. Variables that come out are static variables, with only one copy of the static variables in each class, which is common (each instantiated class is available after assignment)
--Non-static and non-static methods are not accessible in static methods
--Static variables and static methods do not need to be instantiated
1. Non-static member methods and non-static member variables cannot be accessed in a static method, but static member methods/variables can be accessed in non-static member methods. To give a simple example:
2. Can I access static member variables through this?
Although there is no this for static methods, is it possible to access static member variables through this in a non-static method? Let's look at one of the following examples, what is the result of this code output?
| 123456789101112 |
public classMain { staticintvalue = 33; publicstaticvoidmain(String[] args) throwsException{ newMain().printValue(); } privatevoidprintValue(){ intvalue = 3; System.out.println(this.value); }} |
View Code
This is the understanding of the main expedition this and the static. What does this represent? This represents the current object, so the current object is the object generated through new main () by calling Printvalue with new Main (). The static variable is enjoyed by the object, so the value of This.value in Printvalue is undoubtedly 33. The value inside the Printvalue method is a local variable and cannot be associated with this at all, so the output is 33. Always keep in mind that static member variables, while independent of the object, do not mean that they cannot be accessed through objects, and that all static and static variables can be accessed through the object (as long as access is sufficient).
Package
Easy to manage the large number of classes in big software systems, to solve the phenomenon of class name duplicate
Keywords in Java this static package