当前位置:网站首页>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
边栏推荐
- 分割、柱子、list
- MySQL高级篇第一章(linux下安装MySQL)【下】
- Is it safe for individuals to buy stocks with flush software? How to buy stocks
- Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.
- Is it safe to open a fund account? How to apply
- After Ali failed to start his job in the interview, he was roast by the interviewer in the circle of friends (plug)
- Add an "open search description" to the site to adapt to the browser's "site search"“
- Preparation for wechat applet development
- Can string be changed?
- Route jump: click the operation button of the list to jump to another menu page and activate the corresponding menu
猜你喜欢
HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.
栖霞消防开展在建工地消防安全培训
Evaluation - analytic hierarchy process
MySQL addition, deletion, query and modification (primary level)
【好书集锦】从技术到产品
Uni app custom selection date 1 (September 16, 2021)
MySQL高级篇第一章(linux下安装MySQL)【下】
When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
Add an "open search description" to the site to adapt to the browser's "site search"“
随机推荐
进程之间的通信方式
Group note data representation and operation check code
String到底能不能改变?
Kotlin learning apply plugin: 'kotlin Android extensions‘
开通基金账户是安全的吗?怎么申请呢
Uni app custom selection date 1 (September 16, 2021)
WebRTC系列-网络传输之7-ICE补充之偏好(preference)与优先级(priority)
Cliquez sur le bouton action de la liste pour passer à une autre page de menu et activer le menu correspondant
Worm copy construction operator overload
【哈希表】很简单的拉链法哈希结构,以至于效果太差,冲突太多,链表太长
多媒体元素,音频、视频
路由跳轉之點擊列錶的操作按鈕,跳轉至另一個菜單頁面並激活相應的菜單
Uni app custom selection date 2 (September 16, 2021)
Todolist incomplete, completed
“再谈”协议
Class diagram
Sqlitestudio download address
渐变
栖霞消防开展在建工地消防安全培训
上传文件/文本/图片,盒子阴影