当前位置:网站首页>Difference between static method and non static method (advantages / disadvantages)
Difference between static method and non static method (advantages / disadvantages)
2022-07-04 07:48:00 【Lucky_ mzc】
Static methods static( Class method ) and Non static methods ( Example method ) The difference between :
There are three main methods :
1. Construction method
2. Non static methods ( Common method / Example method )
3. Static methods ( Class method )
One 、 The difference between static and non static methods ( Call object 、 Reference variables are different )
Static methods : It's using static Methods of keyword modification , Also called class method . Belonging to the class , Does not belong to the object , Before instantiating an object, you can use Class name . Method name Call static methods . ( Static attribute , Static methods belong to classes , Can be called directly through the class name ).
1. In a static method , You can call static methods .
2. In a static method , Non static methods cannot be called .
3. In a static method , You can reference class variables ( namely ,static Decorated variable ).
4. In a static method , Member variables cannot be referenced ( namely , No, static Decorated variable ).
5. In a static method , Out of commission super and this keyword .
Non static methods : It does not contain static Common methods of keyword modification , Also known as instance method , Member method . Belonging to an object , Not belonging to class .( Member attribute , Member methods belong to objects , Must pass new Keyword after creating an object , And then through the object call ).
1. In the ordinary way , You can call ordinary methods .
2. In the ordinary way , You can call static methods .
3. In the ordinary way , You can reference class variables and member variables .
4. In the ordinary way , have access to super and this keyword .
Two 、 The difference between static and non static methods ( The call method is different )
Static methods can call directly , Class name call and object call .( Class name . Method name / Object name . Method name )
But non static methods can only be called through objects .( Object name . Method name )
3、 ... and 、 The difference between static and non static methods ( Life cycle is different )
The life cycle of a static method is as long as the corresponding class , Static methods and static variables are allocated and loaded into memory as classes are defined . Until the end of the thread , Static properties and methods will be destroyed .( That is, static methods belong to classes )
The life cycle of a non static method is as long as the instantiated object of a class , Only when a class instantiates an object , Non static methods will be created , And when the object is destroyed , Non static methods are also destroyed immediately .( That is, non static methods belong to objects )
summary : Class methods can be called directly by the class name , Instance method must instantiate class first , Reinitialize the object , Then you can call... Through the instance object of the class .
give an example :
class ABC{
public static void main(String[] str){
ABC.testStatic(); // Call... Directly through class
ABC a = new ABC(); // Instantiation , Then the constructor initializes
a.testMethod(); // Object call method
}
public static void testStatic(){
System.out.println("This is static method");
}
public void testMethod(){
System.out.println("This is instance method");
}
}
static state static Variable / Method Initialized during class loading , There is only one copy in memory , So you can think of it as a global variable / Method .
advantage
- Class level , You don't need to create objects to use .
- Globally unique , Unique in memory , Static variables can uniquely identify certain states .
- Initialize when the class is loaded , Resident in memory , Call fast and convenient .
Application scenarios :
1. Static methods are best suited to the definition of methods in tool classes ; Like file manipulation , Date processing method, etc .
2. Static methods are suitable for the definition of entry methods ; As in the singleton mode , Because you can't get the constructor from the outside , It is very necessary to define a static method to obtain objects .
3. Static variables are suitable for the definition of global variables .( For example, Boolean static member variables are used as control characters )
shortcoming
- Static methods cannot call non static methods and variables .( Non static methods can call static methods arbitrarily / Variable )
- Out of commission this and super keyword .( Belong to class level , No object created, check-in is not available this/super)
边栏推荐
- User login function: simple but difficult
- 谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
- This monitoring system can monitor the turnover intention and fishing all, and the product page has 404 after the dispute appears
- [Gurobi] 简单模型的建立
- Handwritten easy version flexible JS and source code analysis
- Put a lantern on the website during the Lantern Festival
- Scanf read in data type symbol table
- 手写简易版flexible.js以及源码分析
- Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
- socket inet_ pton() inet_ Ntop() function (a new network address translation function, which converts the expression format and numerical format to each other. The old ones are inet_aton(), INET_ ntoa
猜你喜欢
墨者学院-phpMyAdmin后台文件包含分析溯源
It's healthy to drink medicinal wine like this. Are you drinking it right
Easy to understand: understand the time series database incluxdb
Sqli labs download, installation and reset of SQL injection test tool one of the solutions to the database error (# 0{main}throw in d:\software\phpstudy_pro\www\sqli labs-...)
Flask 常用组件
Zephyr study notes 2, scheduling
[gurobi] establishment of simple model
zabbix监控系统自定义监控内容
Zephyr 學習筆記2,Scheduling
Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
随机推荐
I was pressed for the draft, so let's talk about how long links can be as efficient as short links in the development of mobile terminals
墨者学院-Webmin未经身份验证的远程代码执行
手写简易版flexible.js以及源码分析
User login function: simple but difficult
Blog stop statement
L1-028 judging prime number (10 points)
Wechat has new functions, and the test is started again
It's healthy to drink medicinal wine like this. Are you drinking it right
socket inet_ pton() inet_ Ntop() function (a new network address translation function, which converts the expression format and numerical format to each other. The old ones are inet_aton(), INET_ ntoa
Email alarm configuration of ZABBIX monitoring system
How to improve your system architecture?
Leetcode 146. LRU 缓存
弈柯莱生物冲刺科创板:年营收3.3亿 弘晖基金与淡马锡是股东
Unity opens the explorer from the inspector interface, selects and records the file path
How to use MOS tube to realize the anti reverse connection circuit of power supply
Rhcsa day 3
L1-026 I love gplt (5 points)
Is l1-029 too fat (5 points)
Unity-写入Word
Introduction to neural network (Part 2)