当前位置:网站首页>Permission modifiers and code blocks
Permission modifiers and code blocks
2022-06-12 09:08:00 【Eden Garden】
One 、 Permission modifier
1、 stay Java Four kinds of access rights are provided in , When decorated with different access modifiers , The decorated content will have different access rights ,
- public: Public
- protected: The protected
- ( Empty ): default
- private: Private
2、 Different access rights
| public | protected | ( Empty ) | private | |
|---|---|---|---|---|
| In the same category | √ | √ | √ | √ |
| In the same bag ( Subclasses and unrelated classes ) | √ | √ | √ | |
| Subclasses of different packages | √ | √ | ||
| Unrelated classes in different packages | √ |
3、 When writing code , Without special consideration , It is recommended to use the permission in this way :
- Member variables use
private, Hidden details .- The construction method uses
public, Easy to create objects .- Member method usage
public, Easy to call methods .
Two 、 Code block
1、 Building blocks of code
Format : {
}
Location : Class , Outside method
perform : Every time you call a constructor , Will execute
Use scenarios : Count how many objects of this type have been created
for example :
public class Person{
{
The construction code block is executed
}
}
2、 Static code block
Format :static{
}
Location : Class , Outside method
perform : When the class is loaded, execute , And only once
Use scenarios : For example, load driver , This kind of code that only needs to be executed once can be placed in static code blocks
public class Person {
private String name;
private int age;
// Static code block
static{
System.out.println(" Static code blocks execute ");
}
}
3、 Local code block
Format :{
}
Location : In the method
perform : Calling method , Execute when the local code block is executed
Use scenarios : Save memory space , It doesn't make much sense
for example :
public static void main(String[] args) {
int a = 10;
// Local code block
// The role of local code blocks is to limit the scope of variables , Disappear from memory early , To save memory
// But now memory is not worth money , So the local code block has no use
{
int b = 20;
}
//int a = 30; // You cannot define a variable with the same name in the same region
// Don't complain , Think the scope is different
int b = 40;
}
边栏推荐
- Basic exercise decomposing prime factors
- (十五) TweenRunner
- 第七章-更灵活定位内存地址
- Load the source code of 2D 3D virtual anchor in the web page (1: project introduction and source code)
- POI library update excel picture
- 域名映射到指定IP
- top命令含义
- (JS) three digits are separated by commas, and two decimal places are reserved (or rounded)
- (node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
- Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
猜你喜欢

EIP-1559

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold

ISCSI详解(五)——ISCSI客户端配置实战

利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包

The classic dog contract of smart contract (I)

Xshell startup encountered "unable to continue code execution because mfc110.dll cannot be found"

Redis installation test

2022 melting welding and thermal cutting test questions and answers

Chapter 8 - two basic problems of data processing
![Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock](/img/42/000a3e601ba1771a1ee07fcd800307.jpg)
Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock
随机推荐
Leetcode 336 palindrome pair (palindrome string + hash)
(js)三位用逗号隔开,保留两位小数(or 四舍五入取整)
Flink passes in custom parameters or profiles
MFS explanation (IV) -- MFS management server installation and configuration
Basic exercise decomposing prime factors
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
2024. maximum difficulty of the exam - sliding window
【字符集九】gbk拷贝到Unicode会乱码?
Construction of memcached cache service under Linux:
【字符集八】char8_t、char16_t、char32_t、wchar、char
(十三)文本渲染Text
Application method of new version UI of idea + use method of non test qualification and related introduction
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
Chapter 7 - more flexible location of memory addresses
Chapter IV - first procedure
(14) Inputfield logic analysis
【字符集七】汉字的宽字符码和多字节码分别是多少
Diff prime pairs
Basic exercise letter graphics
最少换乘次数