当前位置:网站首页>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)
边栏推荐
猜你喜欢

Chrome is set to pure black

The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)

论文学习——基于极值点特征的时间序列相似性查询方法

ZABBIX monitoring system custom monitoring content

JVM中堆概念

在所有SwiftUI版本(1.0-4.0)中原生实现Charts图表视图之思路

This article is enough for learning advanced mysql

PCIE知识点-010:PCIE 热插拔资料从哪获取

Book list | as the technical support Party of the Winter Olympics, Alibaba cloud's technology is written in these books!

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-...)
随机推荐
rapidjson读写json文件
真空介电常数和真空磁导率究竟是由什么决定的?为何会存在这两个物理量?
Go learning notes - constants
Using the rate package for data mining
Tri des fonctions de traitement de texte dans MySQL, recherche rapide préférée
One of the general document service practice series
Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
In the era of low code development, is it still needed?
BibTex中参考文献种类
The idea of implementing charts chart view in all swiftui versions (1.0-4.0) was born
在所有SwiftUI版本(1.0-4.0)中原生实现Charts图表视图之思路
Comparison between applet framework and platform compilation
Activiti常见操作数据表关系
Used on windows Bat file startup project
Linear algebra 1.1
ZABBIX monitoring system deployment
Système de surveillance zabbix contenu de surveillance personnalisé
Activiti common operation data table relationship
Moher College phpmailer remote command execution vulnerability tracing
R language ggplot2 visualization: ggplot2 visualization grouping box diagram, place the legend and title of the visualization image on the top left of the image and align them to the left, in which th