Java construction method and static detailed explanation
1. The main function of the construction method is to initialize
the member variable construction method:
1. There is no return value,
2. The first name is the same as the class name
Note:
1. If you don't write a parameterless construction method, the system will provide you with a parameterless construction method.
2. If I only wrote a structured method with a parameter, then the system no longer provides you with an argument-free construction method
. 3. When writing class, we suggest that the method of parameter-free construction should be written completely.
2. Static variables and static methods (using Ststic retouching) static variables
are stored in the method area's static area (shared area)
static variables are shared interval
static variables how to call
1. You can use the object to call
2. It is recommended to use the class name to invoke the
Why do static variables use class names to
load static variables as long as the. class file is loaded the object does not produce
static variables (the class is also called class variables) and non-static variables (belong to object object variables) difference
1. Different ways of calling c17/> static Variable: Object or class name to invoke (recommended class name)
non-static variable: Only object can be used to invoke
2. Location in memory different
static variables: Static region non-static member variable of method area
: Heap Area
3. Time to load different
static variables: Load (not dependent on objects) Non-static member variables as the class is loaded
: Create (Dependent objects) 4 with the creation of the object
. Different
static variables of life cycle: Destroy with Class (equivalent to end of program)
non-static variables: Destroyed as objects are destroyed
3. Static methods (methods using static modification) Non-static
methods:
1. You can use static and non-static variables 2. Static methods and Non-static methods can be used
:
1. Only static variables
are used 2. Can only use static methods
//Non-static methods to access static variables and non-static variable
//static methods cannot access non-static variables (because the load class does not have objects)
//static can only use static
4.
5.public class Demo {public static void main (string[] args) {Mans man = New Mans ();
Man.fun ();
class man{int num1 = 10;
static int num2 = 5; Non-static methods have access to static and non-static variables//static methods.
(plus the static equivalent of the class change with the object) public void Fun () {System.out.println (THIS.NUM1);
System.out.println (MAN.NUM2);
Call static method (class method): Recommended class name Modulation method Man.sayhi ();
public static void Sayhi () {System.out.println ("I am Sayhi"); Non-static variables cannot be accessed in//static methods//static methods (because there is no object to load the Class)//static can only use static public static void Fun1 () {//SYSTEM.OUT.P
Rintln (NUM1);
System.out.println (NUM2);
Man.sayhi (); }
}