Pretty easy question, but anyway: is there any reason to override default constructor like this:

public SomeObject(){

}

It is public. It is does not have any logic. So, is there necessary? I didn't see the whole picture?

Appreciate all your help.

解决方案

One reason to define an empty no-arg constructor is if there is also a non-default constructor and the no-arg constructor is still desired to be accessible (public or protected).

This is because any [other] constructor definition will prevent the automatic addition of the default no-arg constructor:

The compiler automatically provides a [public] no-argument, default constructor for any class without constructors.

(See the next bit in that link as well, where it talks about the default super constructor that will be called.)

Even if the no-arg constructor is never used manually, it can be important for other things, e.g. Serializable:

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

java 重写构造函数,覆盖Java中的默认构造函数相关推荐

  1. java重写(覆盖)与重载的区别

    java重写(覆盖)与重载的区别 1.重写(覆盖Override) 重写发生在接口与实现类,父类与子类之间,特点如下: 方法名,返回值类型必须相同,参数列表必须相同 访问权限只能等于或者大于父类的访问 ...

  2. C++中若类中没有默认构造函数,如何使用对象数组

    前言: 如果定义一个类,有其默认的构造函数,则使用new动态实例化一个对象数组,不是件难事,如下代码: 1 #include <memory> 2 #include <iostrea ...

  3. java 默认参数 实例化_如何使用Kotlin中的默认构造函数参数值实例化对象?

    使用在科特林: 在构造函数中的默认值仅服用时该构造的特定参数不传递到它在所有.那意味着它是为了实现各种组合的参数化构造函数. 例如, data class Bird (val name: String ...

  4. java隐藏与覆盖_java中方法的隐藏和覆盖问题?

    ok()方法在继承时候发生覆盖(重写)了吗? 发生了.在子类B中的ok()方法拥有父类方法相同的方法名和参数列表(signature),所以在这里发生了重写.调用B类对象的ok()方法会print & ...

  5. java默认构造函数_Java中的默认构造函数

    java中如果在一个类中没有写明任何构造函数的,那么会存在一个无参的构造函数,如下: public class Children { private String name; private Stri ...

  6. java默认函数参数是什么意思,什么时候必须在Java中使用默认构造函数和参数化构造函数?...

    Many a time I have got an exception saying "the default constructor's implementation is missing ...

  7. java socket超时时间_java中socket 默认超时时间 | 学步园

    MS:永远吧 Java代码 connect publicvoidconnect(SocketAddress endpoint, inttimeout) throwsIOException Connec ...

  8. java final被覆盖_java中的final的使用

    1.final类不能被继承,因此final类的成员方法没有机会被覆盖,默认都是final的.在设计类时候,如果这个类不需要有子类,类的实现细节不允许改变,并且确信这个类不会再被扩展,那么就设计为fin ...

  9. [转载] Java中使用new构造数组时会不会自动调用类的默认构造函数

    参考链接: Java中的默认构造函数 今天做集合练习时,发现生成类的数组时并 不会自动调用类的默认构造函数 // 开辟10个内存空间,并指定了数组的类型为Gerbil // 这个时候并不会调用构造函数 ...

最新文章

  1. 详解python2 和 python3的区别
  2. BZOJ 4974: 字符串大师
  3. Github(2)-本地配置git
  4. LeetCode 1690. 石子游戏 VII(博弈DP)
  5. python怎么读写_python怎么读写文件
  6. Mybatis中的缓存详解
  7. c9, Performance Monitor Control Register
  8. 微信小程序-组件使用
  9. KindEditor上传图片
  10. 简要html漂浮广告代码,JS漂浮广告代码
  11. 内存颗粒的逻辑bank理解
  12. Chrome 页面呈现原理与性能优化(内附分享 ppt)
  13. Android DNS之gethostbyname()的实现
  14. 4、(四)外汇学习基础篇之银行间外汇远期交易
  15. input标签只能输入数字
  16. 雷达图人格php源码,061 实例15-霍兰德人格分析雷达图
  17. [转]python各模块的学习
  18. 【BZOJ3893】【Usaco2014 Dec】Cow Jog 乱搞
  19. 手机界面设计中12种常用布局
  20. 科学等于艺术,艺术等于科学

热门文章

  1. 机器翻译评测——BLEU算法详解 (新增 在线计算BLEU分值)评估
  2. LeetCode简单题之交替合并字符串
  3. NVIDIA深度学习Tensor Core性能解析(下)
  4. ❤️让人心跳加速的陌陌案例,大数据必需学会的基础案例!❤️ 【推荐收藏】
  5. logcat --pid xx 查看某个进程的信息
  6. 微信小程序setData()方法的使用
  7. Error on line 19, column 16 of pubspec.yaml: Mapping values are not allowed here. Did you miss a co
  8. day07-字符编码、文件操作
  9. hadoop学习之旅1
  10. 2022-2028年中国智能制造装备产业深度调研及投资前景预测报告