life cycle :
Static method : Same as static member variables , Belongs to the class itself , It is loaded into memory when the class is loaded , No automatic destruction , It will always be in memory , know JVM close .
Non static method : Also called instantiation method , Belongs to instance object , Memory will not be allocated until instantiated , Must be referenced through an instance of a class . Does not reside in memory , When the instance object is JVM After recycling , And disappear .
Static method is more efficient than non-static method .
According to the above concept , As long as there's enough memory , You can use static . What are the problems with using static methods .
Static method : Is a shared code snippet , Static variables are shared data segments . Since it is ” share ” There is concurrency .
Non static method : It is for a certain object , So there is no thread safety issue .
The static method is the same as the instance method , Load when the type is first used . There is basically no difference in the speed of calls .
for example :
* Objects that do not need to be generated Frequently used In the tool class （ as SqlHelper）
* I understand that methods that need to be called in multiple classes and are object independent can be set as static methods .
Advantages and disadvantages :
In some systems, using very trivial methods can improve system performance by using static methods .
It cannot be upgraded , rewrite , It depends on the purpose of a software product .
for convenience , No instantiation , But in this way, the compile and run time of the program takes up the system resources .
Static properties and methods at the start of the program , It's all loaded into memory , Regardless of these methods , Is the attribute used in the future . Even if no one is accessing the program , This memory will not be released . And then there is , All visitors see almost the same data for static properties , such as A User set UserName This property ,B When users visit , obtain UserName Still A User set that . This characteristic , If used in fixed data , That won't be a big problem .
:method（ Method area ） Also called static area , Store all ① class （class）,② Static variable （static variable ）,③ Static method ,④ Constant sum ⑤ Member method ( It's the common method , Modifier by access , return type , Class name and method body composition ).
jvm There is only one heap (heap) Shared by all threads , Base types and object references are not stored in the heap , Store only the object itself .
Java The area of the stack is very small , only 1M, It is characterized by high access speed , So in the stack All of them are fast-moving tasks , A stack frame is created when each method is executed （Stack
Frame）, The description is java Memory model of method execution , Used to store local variables , Operands stack , Method export , Data of basic data type , And object references （reference）
etc . Each method call has the corresponding out and in stack .
Program counter （ProgramCounter） register :
PC register （ PC register ）： When each thread starts , Will create one PC（Program
Counter, Program counter ） register .PC The register contains the currently executing JVM The address of the instruction .
Runtime Constant Pool :
Is part of the method area .Class In addition to the version of the class in the file , field , method , Interface and other descriptive information , Another piece of information is the constant pool , Used to store various literal quantities and symbol references generated by the compiler , This part will be stored in the runtime constant pool in the method area after the class is loaded .
Native Method Stack :
Local method stack and Java Virtual machine stacks play a very similar role , The difference between them is that the virtual machine stack executes for the virtual machine java method ( It's a bytecode file ) service , The local method stack uses the Native Method service .