In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, Xiaobian will bring you about static analysis. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.
static summary
static Fields
static Methods
static member class
static initializer
--------------------------------------------------------------------------------
static member invocation method:
static is a modifier used to modify members (member variables, member functions, and member classes/interfaces)
When a member is static, there is one more way to call it.
In addition to being called by objects, it can also be called directly by class names.
Call format: Class name. static members
1)New object or not, can be called by: class name. Static member (recommended)
2)Must be called after new object: object. Static members (not recommended, bad reading)
--------------------------------------------------------------------------------
static Member characteristics:
> Load as classes load, disappear as classes disappear
That is, static members have the longest life cycle (equivalent to the life cycle of a class)
> exists prior to objects
Static members are pre-existing and objects are post-existing.
> Shared by all objects
> Can be called directly by class name
--------------------------------------------------------------------------------
Difference between instance variables and class variables:
1. storage location
Class variables are stored in the method area as the class is loaded
Instance variables are stored in heap memory as objects are created
2. life cycle
Class variables have the longest life cycle, load with the class load, disappear and disappear
Instance variable life cycle is created with the creation of objects, disappears and disappears
3. calling method
Instance variables can only be invoked through objects
Class variables can be called either by object or class name.
--------------------------------------------------------------------------------
When to use static?
When the value of a member variable is shared by all objects of the class, it is modified with static, that is, it is defined as a class variable
Object is defined as instance variables and exists in heap memory
static when a member function does not operate on any member variables or operates only on static member variables
What about member classes/interfaces??? What about static member class?
--------------------------------------------------------------------------------
static use precautions
> Static Only static can be accessed
Non-static can be accessed static or non-static
> Static keyword this, super cannot be used
Because static takes precedence over object existence, the keyword this super cannot be used in static
> The main function is static
--------------------------------------------------------------------------------
static advantages and disadvantages
Benefits:
Storage of shared data for all objects in separate spaces saves space. There is no need to store a copy of each object.
Can be called directly by class name
Disadvantages:
Long life cycle
Access is limited (static is good, only static can be accessed)
--------------------------------------------------------------------------------
Definition of main function
public static void main(String[] args) {}
The main function is a special function. As an entry point to a program, it can be invoked by the JVM.
The main function must be in fixed format to be recognized by the JVM.
Main function definition:
public: Indicates that access to the function is cross-package
static: means that the main function is loaded as the class is loaded
No need to create an object, directly with the main function belongs to the class name can be called.
When you type java StaticDemo in the command line window, you are actually executing Static.main(String[])
void: The main function has no specific return value
main: Not a keyword, but a special word that can be recognized by the JVM.
(String[] args): List of arguments to the main function. Parameter type is an array of string type.
Main function also has the characteristics of general functions, can be overloaded.
But the JVM calls only main functions whose parameter list is String[].
The parameter name of main function can be changed and still be recognized by JVM.
How to pass parameters to JVM via main function
--------------------------------------------------------------------------------
static applications-tool classes
There are common features in every application. These functions can be extracted and packaged separately for reuse.
--------------------------------------------------------------------------------
static code block
Declaration format:
static {
Execution statements;
}
Features:
The loading of a class can be triggered when the// new object is executed with the loading of the class or when a static member of the class is referenced
You can verify whether a class is loaded by adding a static code block
Execute only once//because class loads only once
Preempts execution of the main function//because the class in which the main function is located must be loaded before execution of the main function
Only static members of a class can be accessed, not non-static members
Function:
Used to initialize classes
--------------------------------------------------------------------------------
When can I trigger class loading?
new objects or static members of a class are referenced to trigger the loading of the class
Creating only reference variables of class types does not trigger class loading
The static code block of the class to which the main function belongs takes precedence over the main function execution
--------------------------------------------------------------------------------
static code block, constructor code block and constructor comparison
(1)static code block: used for class initialization. Executes with class load and only once.
(2)Construction code block: Used for all object initialization. new An object executes once.
(3)Constructor: corresponds to object initialization. The constructor is executed once if new corresponds to the object.
(4)Access restrictions:
static code blocks can only access static members;
Constructor blocks can call static and nonstatic members, but not constructors;
Constructors can call static, nonstatic, and other constructors.
(5)Execution order:static code block-> construction code block-> corresponding constructor
When the class is loaded, do the initialization action of the class, execute and execute the static code block only once;
new object, if the class is not loaded, load the class first, if it is loaded, do the initialization action of the object, execute the construction code block in turn, corresponding to the constructor.
ps: new objects do not necessarily load classes, because classes may already be loaded.
The above is how to carry out static analysis shared by Xiaobian for everyone. If there are similar doubts, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.