当前位置:网站首页>Comparison of static methods and variables with instance methods and variables
Comparison of static methods and variables with instance methods and variables
2022-06-26 03:44:00 【yuyue5945】
C# Commonly used Static Modify static variables and static methods
What is the difference between static variables and instance variables ?
- 1、 Grammatically : Static variables are preceded by Stati keyword , Instance variables do not require .
- 2、 Runtime : When the program runs, the static variables will be allocated space , The instance variable belongs to the attribute of an object , You need to create an instance object to allocate space .
- 3、 Storage area : Static variables are stored in static storage , Ordinary variables are stored in the heap .
There are two main differences between static method and instance method :
When a static method is called externally , have access to " Class name . Method name " The way , You can also use " Object name . Method name " The way . And the instance method only has the latter way . in other words , You can call static methods without creating objects .
Static methods when accessing members of this class , Only static members are allowed ( Static member variables and static methods ), Access to instance member variables and instance methods is not allowed ; There is no limit for instance methods .
Thread safety of static methods
Let's briefly discuss the use of static fields in a class (static field) And static methods (static method) Whether there will be thread safety problems .
We're learning , Static field (static field) And static methods (static method) Is called through a class . Static methods do not operate on specific instances , Only static members can be accessed . Instance methods can operate on specific instances , Can access static members , Can also access instance members .
that , Whether using static methods in multithreading has thread safety problems ? This depends on whether static methods cause thread safety problems and whether static members are used in static methods .
because , When using the same static method in multiple threads , Each thread uses its own instance field (instance field) Copy of , And share a static field (static field). So , If the static method does not operate on a static member , Use instance fields only inside methods (instance field), Will not cause security problems . however , If the static method operates on a static field , You need to use mutually exclusive access in static methods for security processing .
A simple example , What we use Console.WriteLine(); in WriteLine() yes Console.WriteLine Class static methods .
If static methods do not use static variables , There are no thread safety issues .
Why? ? Because variables declared in static methods , When each thread calls , Will create a new , Instead of sharing a storage unit . Like here tmp, Each thread creates its own , So there's no thread safety issue .
Be careful : Static variables , Because it occupies a storage area when the class is loaded , Each thread shares this store , So if you use static variables in static methods , There will be thread safety issues !
Blogger GitHub Address
https://github.com/yuyue5945
Pay attention to the official account

边栏推荐
- 2022.6.25-----leetcode.剑指offer.091
- Sqlitestudio download address
- 链路监控 pinpoint
- MySQL stored procedure
- 2022.6.25 - leetcode. Un doigt d'épée. 091.
- Dynamic segment tree leetcode seven hundred and fifteen
- 微信小程序开发准备工作
- [hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time
- 面试阿里测开岗失败后,被面试官在朋友圈吐槽了......(心塞)
- [paper notes] supersizing self supervision: learning to grasp from 50K tries and 700 robot hours
猜你喜欢

进度条

拖放

todolist未完成,已完成

多媒体元素,音频、视频

. Net core learning journey

Solve the problem that the uniapp plug-in Robin editor reports an error when setting the font color and background color
HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct

解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题

Mysql database foundation

Uni app, the text implementation expands and retracts the full text
随机推荐
The kotlin project is running normally and the R file cannot be found
An easy-to-use tablayout
Binary search
js实现文字跑马灯效果
Xgboost, lightgbm, catboost -- try to stand on the shoulders of giants
渐变
mysql 常用语句
Dynamic segment tree leetcode seven hundred and fifteen
Is it safe for individuals to buy stocks with flush software? How to buy stocks
路由跳轉之點擊列錶的操作按鈕,跳轉至另一個菜單頁面並激活相應的菜單
HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
Qixia fire department carries out fire safety training on construction site
Slide the menu of uni app custom components left and right and click switch to select and display in the middle
开通基金账户是安全的吗?怎么申请呢
Tupu software is the digital twin of offshore wind power, striving to be the first
Popupwindow utility class
Uni app Baidu cloud realizes OCR ID card recognition
[paper notes] learning to grasp with primitive shaped object policies
MySQL高级部分( 四: 锁机制、SQL优化 )
816. 模糊坐标