When all the required work can be done inside the constructor, then why do we still need non-static block in Java?

EDIT: What about normal classes for which non-static blocks run everytime before the constructor?

解决方案

In additional to @Bohemian's answer.

If you have multiple constructors an intialiser block avoid duplication.

public class A {

final Map map = new HashMap(); {

// put things in map.

}

final int number; {

int number;

try {

number = throwsAnException();

} catch (Exception e) {

number = 5;

}

this.number = number;

}

public A() { /* constructor 1 */ }

public A(int i) { /* constructor 2 */ }

}

What about normal classes for which non-static blocks run everytime before the constructor?

Technically the order is

the super constructor is always called first

all the initializer blocks in order of appearence.

the constructor code

In reality all this code is in the byte code for each constructor so there is nothing before or after the constructor at runtime.

Before there is any more confusion as to the order of initialization

public class Main extends SuperClass {

{

System.out.println("Initialiser block before constructor");

}

Main() {

System.out.println("Main constructor");

}

{

System.out.println("Initialiser block after constructor");

}

public static void main(String... args) {

new Main() {{

System.out.println("Anonymous initalizer block");

}};

}

}

class SuperClass {

SuperClass() {

System.out.println("SuperClass constructor");

}

}

prints

SuperClass constructor

Initialiser block before constructor

Initialiser block after constructor

Main constructor

Anonymous initalizer block

java非静态块,在java中使用非静态块有什么用?相关推荐

  1. php正则表达式非贪婪,php正则表达式中的非贪婪模式匹配

    php正则表达式中的非贪婪模式匹配 一般情况下,正则表达式的匹配是贪婪模式的,比如下面这个例子: 字符串:....src="http://www.bloghome.cn/1.mp3" ...

  2. java公平索非公平锁_java中的非公平锁不怕有的线程一直得不到执行吗

    首先来看公平锁和非公平锁,我们默认使用的锁是非公平锁,只有当我们显示设置为公平锁的情况下,才会使用公平锁,下面我们简单看一下公平锁的源码,如果等待队列中没有节点在等待,则占有锁,如果已经存在等待节点, ...

  3. php mysql 非扫描,PHP的中使用非缓冲模式查询数据库的方法

    最近在开发一个PHP程序时遇到了下面的错误: PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted 错误信息显示允许的 ...

  4. python请求url非阻塞_Tornado请求中的非阻塞/异步URL获取

    我已经从我的项目中提取了代码样本.它并不完美,但它提供了一个如何使用Tornadp的AsyncHTTPClient的方法@tornado.gen.engine def async_request(se ...

  5. java接口有非抽象方法_[Java教程]纳尼,java可以在接口中实现非抽象方法了?

    [Java教程]纳尼,java可以在接口中实现非抽象方法了? 0 2016-09-17 18:00:20 纳尼,接口中可以定义实例方法了?! 纳尼,接口中还可以定义静态方法了?! 没错,在Java8中 ...

  6. java中如何限制输入非空_项目中的参数限制、非空、以及集合中的验证,你真的会做吗(Java)...

    背景: 在项目管理中,我们通常要对一个个参数做验证, 前端校验,后端加校验,这里为了保证校验的一致性,我们需要将我们的校验写的比较高效率一点,不至于满篇都是is null的进行参数验证,为了成为一个成 ...

  7. java中访问修饰符_Java中的非访问修饰符是什么?

    java中访问修饰符 Java非访问修饰符 (Java non access modifiers) We have 7 non-access modifiers in Java. The name o ...

  8. JAVA中的线程安全与非线程安全,java面试题,java高级笔试题

    写在最前面,我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家.扫码加微信好友进[程序员面试学习交流群],免费领取.也欢迎各位一起在群里探讨技术. 转自 ...

  9. 求集合中的非空子集 Java

    字符串的非空子集 import java.util.*; public class _非空子集 {public static List<String> sList = new ArrayL ...

  10. java中怎样调用抽象类中的非抽象方法?(对原文做修改,加入一点自己理解)

    问: 例如:我有一个抽象类BaseStudentDAO,类中有一个非抽象方法load,我该怎样调用load这个方法? 答: 想调用BaseStudentDAO中的非抽象方法load,如果load方法是 ...

最新文章

  1. 我的软考大事记(北京市)
  2. VC中的Attach和Detach
  3. .NET:命令行解析器介绍
  4. TWaver html5 + NodeJS + express + websocket.io + redis 快速搭建项目(一)
  5. 6条策略提高mysql查询速度 潇湘博客
  6. 如何调试栈破坏之类的问题
  7. 耶鲁大学计算机科学录取,耶鲁大学计算机科学研究生Offer及录取要求
  8. 电脑重装系统后没声音怎么办
  9. 浪漫的表白 (5 分)
  10. 用户态程序阻塞原因_进程阻塞 操作系统某种情况进行进程的阻塞和唤醒操作...
  11. linux x11 挖矿病毒,应急响应:记一次花样贼多的挖矿病毒
  12. 浅谈压缩感知(二十二):压缩感知重构算法之正则化正交匹配追踪(ROMP)
  13. pr、ps、ae-安装
  14. 兆骑科创平台创新创业赛事路演,投融资服务
  15. Windows10+YOLOV3+VisualStudio2017最新版本超详细过程
  16. Spring配置数据库密码加密
  17. 微信小程序输入框字数限制以及计算
  18. openlayers加载Mapbox底图
  19. rhel6的部分版本使用intel xeon处理器时的bug
  20. stc8a控制MG90S舵机

热门文章

  1. 为什么在中国“公有云”落地那么难?
  2. Python 精要参考(第二版) 第六章 函数与函数编程
  3. Coinbase宣布在以太坊经典上市前进行最终测试
  4. 又一款基于BCH开发出来的社交软件BlockPress
  5. 利好不断,跑步入场支持BCH
  6. iOS多线程:『NSOperation、NSOperationQueue』详尽总结
  7. Docker的安装和镜像管理并利用Docker容器实现nginx的负载均衡、动静分离
  8. 【mysql】Mac下安装mysql5.7 完整步骤,大坑已解决
  9. 实战2--应用EL表达式显示投票结果
  10. C# 关键字Event