Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
cppg
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
object-oriented
/
variable.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
35 lines (28 loc) · 1.47 KB
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
object-oriented
/
variable.md
Copy path
Top
File metadata and controls
Code
Blame
35 lines (28 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Java中共有三种变量
,
分别是类变量
、
成员变量和局部变量
。
他们分别存放在JVM的方法区
、
堆内存和栈内存中
。
```
java
/**
* @author Hollis
*/
public
class
Variables
{
/**
* 类变量
*/
private
static
int
a
;
/**
* 成员变量
*/
private
int
b
;
/**
* 局部变量
* @param c
*/
public
void
test
(
int
c
){
int
d
;
}
}
```
上面定义的三个变量中
,
变量a就是类变量
,
变量b就是成员变量
,
而变量c和d是局部变量
。
a作为类变量
,
他存放在方法区中
;
b作为成员变量
,
和对象一起存储在堆内存中
(
不考虑栈上分配的情况
);
c和d作为方法的局部变量
,
保存在栈内存中
。
之所以要在这一章节重点介绍下这三种变量类型
,
是因为很多人因为不知道这三种类型的区别
,
所以不知道他们分别存放在哪里
,
这就导致不知道那些变量需要考虑并发问题
。
关于并发问题
,
目前本书
《
基本篇
》
还不涉及
,
会在下一本
《
并发篇
》
中重点介绍
,
这里先简单说明一下
:
因为只有共享变量才会遇到并发问题
,
所以
,
变量a和b是共享变量
,
变量c和d是非共享变量
。
所以如果遇到多线程场景
,
对于变量a和b的操作是需要考虑线程安全的
,
而对于线程c和d的操作是不需要考虑线程安全的
。
You can’t perform that action at this time.