What is Instance Initializer in Java?
来源:互联网 发布:小约翰 震惊 知乎 编辑:程序博客网 时间:2024/05/02 04:36
In this post, I will first use an example to show what are instance variable initializer, instance initializer and static initializer, and then illustrate how the instance initializer works in Java.
1. Execution Order
Look at the following class, do you know which one gets executed first?
Output:
static initializer calledinstance initializer calledconstructor calledinstance initializer calledconstructor called
2. How does Java instance initializer work?
The instance initializer above contains a println statement. To understand how it works, we can treat it as a variable assignment statement, e.g., b = 0
. This can make it more obvious to understand.
Instead of
, you could write
Therefore, instance initializers and instance variable initializers are pretty much the same.
3. When are instance initializers useful?
The use of instance initializers are rare, but still it can be a useful alternative to instance variable initializers if:
(1) initializer code must handle exceptions
(2) perform calculations that can't be expressed with an instance variable initializer.
Of course, such code could be written in constructors. But if a class had multiple constructors, you would have to repeat the code in each constructor.
With an instance initializer, you can just write the code once, and it will be executed no matter what constructor is used to create the object. (I guess this is just a concept, and it is not used often.)
Another case in which instance initializers are useful is anonymous inner classes, which can't declare any constructors at all. (Will this be a good place to place a logging function?)
Thanks to Heinrich Hartmann for his comment:
Also note that Anonymous classes that implement interfaces [1] have no constructors. Therefore instance initializers are neede to execute any kinds of expressions at construction time.
Reference:
Object initialization in Java
- What is Instance Initializer in Java?
- What is CountDownLatch in Java
- What is CyclicBarrier in Java
- Java - What is final in Java?
- Java - What is Abstraction in Java
- 43.In your database instance, the STATISTICS_LEVEL initialization parameter is set to BASIC. What is
- What exactly is null in Java?[转]
- What is autoboxing and unboxing in Java
- Seed() in initializer is not called
- what is Class Object(java.lang.class) in java?
- What is volatile variable in Java - When to use
- What is Daemon Thread in Java? Example Attached
- What is a Context in Java and ApplicationContext vs WebApplicationContext
- What is difference between HashMap and Hashtable in Java?
- what is #pragma in c
- What is uadmin in Solaris?
- What is MultiSampleQuality in D3D
- what is Activity in Android
- 小球下落
- python_轻量级爬虫开发4
- Activity和Service通信
- JavaScript之函数和this
- python 学习笔记3
- What is Instance Initializer in Java?
- 学习记录
- UVA 1608 Non-boring sequences (递归分治)
- 动态规划-三角形
- 在Activity中使用Toast
- QTcpSocket的读写操作
- 并查集+欧拉回路
- ubuntu+opencv+qt小试牛刀2人脸识别
- bzoj3529: [Sdoi2014]数表