constructor function:
1. The function name is the same as the class name, no return value type is defined, no specific return value
2. The function that is called when the object is created, the creation object must be initialized by the constructor function.
3. When you use a custom constructor, you must write a default constructor
The difference between a constructor and a normal function:
1. Constructors are called when an object is created, and the object is initialized
2. The normal function is called when the function is needed after the object is created
3. Constructors are called only once when an object is created
4. The normal function can be called repeatedly
5. Constructors can call normal functions, while normal functions cannot call constructors
6. The constructor can write return, but be careful not to write return value;
When to use constructors:
When a thing is described, there is something in it that is defined in the constructor
Constructor overloading:
Same as the normal function overload, that is, the function has a different number of arguments or types, the name of the functions is exactly the same
Rewrite:
The function name and the parameter number and the type are identical, the concrete realization content is different
This keyword:
1. When member variables and local variables have the same name , you can use the This keyword to differentiate
2. Essentially: this represents the object (the current object), which is the reference to the object to which the function belongs
In simple terms, which object calls The function where this is located, which object is represented by this
3. The This keyword is also used to call other constructors in the constructor
Note: You can only define the first row in the constructor, because the initialization action is performed first
Static Keyword:
1. After the data is modified by static , the data is saved in heap memory.
2. In a class, data shared by all objects is generally initialized to static data
3. Static data is initialized in the heap memory before the object is instantiated. Which is initialized as the class is loaded.
4. Static modified members are shared by all objects
5. Static takes precedence over the existence of the object because the static member already exists as the class is loaded.
6. Static modified members can be called by the object or directly by the class name (class name . Static members)
7. Static modified data is shared data, objects are stored in unique data
Member variables & instance variables & static variables:
1. A member variable is also called an instance variable; A static variable is also called a class variable
2. The member variable exists as the object exists, and dies as the object dies;
3. Static variables exist as classes are loaded and disappear as classes disappear
4. Member variables can only be called by the object, and static variables may be called by the object or by the class (strongly not recommended
Use object invocation because a static member precedes the object exists)
5. Member variable data is stored in the object of the heap memory, so it is also called the object's unique data
6. Static variable data is stored in the shared data area, so it is also called an object's shared data.
Memory Classification: Local method area, register, shared data area (method area), heap, stack
Static methods can access only static members (member variables and member methods), and non-static methods can access both static and non-static
The This or Super keyword cannot be used in a static method because the static method is called by the class name, is not dependent on the object, and therefore cannot be used, and the constructor method is non-static and therefore cannot be used with super Key Words
Main function:
1. The main function is static
2. What is special about the main function: fixed format, recognized and invoked by the JVM virtual machine
3. The main function is public and the permissions must be the largest
4. The main function must be staticbecause the main function does not need to be called by the object
4. The main function must be void, with no specific return value
5. Main: function name, not keyword, just a fixed name identified by the JVM
6. string[] args:args is a parameter list of the main function, is an array type parameter, and the element is a string type.
When do I use the static modifier?
1. Static variables: When the parsed object has the same values, the member can be statically decorated. As long as the data
object is different, is the object's unique data, must be stored in the object, non-static, if the same data,
objects do not need to be modified, they need to be used, they do not need to be stored in objects, they are defined as static.
2. Static function: Whether the function is static decorated, only to refer to a point, the function function has access to the object's unique data. Jane
Single point of view, from the source code, whether the function requires access to non-static members, if necessary, the function is non-static;
The function can be defined as static, of course, it can also be defined as non-static, but non-static needs to be
A method that does not have access to unique data, like calls, but only the creation of an object access non-static method, does not make sense when the object is created.
Static code block:
1. Executes as the class loads and executes only once (note: The class is loaded only once, and even if the object is executed two times, it does not
Execute two times because only one time is loaded)
2. Function: Used to initialize a class
3. For example
Class Staticcode
{
static int age = 0;
static {
Age = age + 20;
}
static void Show ()
{
System.out.println (age);
}
}
Class Staticcodedemo
{
public static void Main (string[] args)
{
Staticcode.show ();
}
}
static code blocks are used less often and static blocks of code may be required when the class is full of static members .
To construct a code block:
1. Define a block of code that is not static-decorated in a class, that is, to construct a block of code
2. Unlike static blocks of code, constructing blocks of code can initialize all objects, each time the object is created, executed before the constructor method is called, and the construction method is used to initialize the corresponding object with a specific initialization.
Local code block:
1. Defined in the method
2. Used to limit the life cycle of local variables
This article is from the "Keyman" blog, make sure to keep this source http://keyman.blog.51cto.com/9807984/1676599
7. javase-constructor & this & static keyword