java静态构造函数

Java Static Constructor is not allowed, but why? Before we dig into the reasons for not allowing static constructor, let’s see what happens if we want to make a constructor static.

不允许使用Java静态构造函数,但是为什么呢? 在深入探讨不允许使用静态构造函数的原因之前,让我们看看如果要使构造函数静态化会发生什么。

Java静态构造函数 (Java Static Constructor)

Let’s say we have a class defined as:

假设我们有一个定义为的类:

public class Data {private int id;public static Data() {}
}

If you will try to compile this class, you will get an error message as Illegal modifier for the constructor in type Data; only public, protected & private are permitted.

如果您尝试编译此类,则会在Data类型的构造函数中收到一条错误消息,作为非法的修饰符。 仅允许公开,受保护和私有

为什么不允许使用静态构造函数? (Why Static Constructor is not allowed?)

Let’s see some of the reasons that make the compelling arguments for not allowing static constructor in java.

让我们来看看一些引人入胜的原因,原因是不允许在Java中使用静态构造函数。

静态属于类,构造函数属于对象 (Static Belongs to Class, Constructor to Object)

We know that static methods, block or variables belong to the class. Whereas a Constructor belongs to the object and called when we use the new operator to create an instance. Since a constructor is not class property, it makes sense that it’s not allowed to be static.

我们知道静态方法,块或变量属于该类。 构造函数属于该对象,并在使用new运算符创建实例时调用。 由于构造函数不是类属性,因此有理由认为它不是静态的。

静态块/方法无法访问非静态变量 (Static Block/Method can’t access non-static variables)

We know that static methods can’t access non-static variables. Same is true for static block also.

我们知道静态方法不能访问非静态变量。 静态块也是如此。

Now, the main purpose of a constructor is to initialize the object variables. So if we make constructor as static then it won’t be able to initialize the object variables. That will defeat the whole purpose of having a constructor for creating the object. So it is justified to have the constructor as non-static.

现在,构造函数的主要目的是初始化对象变量。 因此,如果我们将构造函数设为静态,则它将无法初始化对象变量。 这将使拥有用于创建对象的构造函数的整个目的无效。 因此,使构造函数为非静态是合理的。

Notice that we can’t use this inside a static method to refer to the object variable. Below code will give compilation error as: Cannot use this in a static context.

注意,我们不能在静态方法中使用this来引用对象变量。 下面的代码将给出编译错误,因为: 不能在静态上下文中使用它

public static void main(String args[]) {System.out.println(this.id);
}

静态构造函数将破坏继承 (Static Constructor will break inheritance)

In Java, every class implicitly extends Object class. We can define a class hierarchy where subclass constructor calls the superclass constructor. This is done by super() method call. Most of the times JVM automatically calls the superclass constructor but sometimes we have to manually call them if there are multiple constructors in the superclass.

在Java中,每个类都隐式扩展了Object类。 我们可以定义一个类层次结构,其中子类构造函数调用超类构造函数。 这是通过super()方法调用完成的。 大多数情况下,JVM自动调用超类构造函数,但有时如果超类中有多个构造函数,我们必须手动调用它们。

Let’s see an example for super() usage.

让我们看一个super()用法的例子。

package com.journaldev.util;class Data {Data() {System.out.println("Data Constructor");}
}public class DataChild extends Data{public DataChild() {super(); //JRE calls it explicitly, calling here for explanationSystem.out.println("DataChild Constructor");}public static void main(String args[]) {DataChild dc = new DataChild();}
}

Above program will produce the following output.

上面的程序将产生以下输出。

Data Constructor
DataChild Constructor

If you look at the super() method, it’s not static. So if the constructor becomes static, we won’t be able to use it and that will break inheritance in java.

如果您看一下super()方法,它不是静态的。 因此,如果构造函数变为静态,我们将无法使用它,这将破坏java中的继承 。

Java静态构造方法替代 (Java Static Constructor Alternative)

If you want to initialize some static variables in the class, you can use static block. Note that we can’t pass arguments to the static block, so if you want to initialize static variables then you can do that in the normal constructor too.

如果要在类中初始化一些静态变量,则可以使用静态块。 请注意,我们无法将参数传递给静态块,因此,如果您要初始化静态变量,则也可以在常规构造函数中执行此操作。

class Data {public static int count;static {count = 0;}Data(int c) {//not recommended since the count is class variable //and shared among all the objects of the classcount=c; }
}

摘要 (Summary)

Java static constructor is not allowed and we have very good reasons for that. We can initialize static variables using the static block as well as through constructor itself.

不允许使用Java静态构造函数,对此我们有充分的理由。 我们可以使用静态块以及构造函数本身来初始化静态变量。

翻译自: https://www.journaldev.com/22432/java-static-constructor

java静态构造函数

java静态构造函数_为什么不允许使用Java静态构造函数?相关推荐

  1. java核心面试_不正确的核心Java面试答案

    java核心面试 总览 在Internet上,Java面试问题和答案从一个网站复制到另一个网站. 这可能意味着错误或过时的答案可能永远不会得到纠正. 这是一些不太正确或已经过时的问题和答案. 即是Ja ...

  2. java rest 序列化_一文看懂Java序列化

    一文看懂Java序列化 简介 首先我们看一下wiki上面对于序列化的解释. 序列化(serialization)在计算机科学的数据处理中,是指将数据结构或对象状态转换成可取用格式(例如存成文件,存于缓 ...

  3. java lock 效率_工作常用4种Java线程锁的特点,性能比较、使用场景

    多线程的缘由 在出现了进程之后,操作系统的性能得到了大大的提升.虽然进程的出现解决了操作系统的并发问题,但是人们仍然不满足,人们逐渐对实时性有了要求. 使用多线程的理由之一是和进程相比,它是一种非常花 ...

  4. java 面试 概率论_编程培训-115个Java面试题和答案B.pdf

    编程培训-115个Java面试题和答案B.pdf "玩转"Java系列 1 题目115个Java面试题和答案终极(下) 第一篇讨论了面向对象编程和它的特点,关于Java和它的功能的 ...

  5. java coin介绍_代码示例中的Java 7:Project Coin

    java coin介绍 该博客通过代码示例介绍了一些新的Java 7功能,这些项目在Project Coin一词下进行了概述. Project Coin的目标是向JDK 7添加一组小的语言更改.这些更 ...

  6. java是纯_让你真正了解Java(纯干货)

    "你学习一门技术的最佳时机是三年前,其次是现在."这句话对于哪一种行业都很适用,如果你已经学习过Java,那么恭喜你你很有先见之明,如果你并不了解Java,这篇文章带你快速掌握Ja ...

  7. java arraylist排序_一文读懂Java集合框架

    欢迎关注微信公众号:深入浅出Java源码 概念 Java集合框架为程序员提供了预先包装的数据结构和算法来操纵他们.集合框架被设计成要满足以下几个目标. 该框架必须是高性能的.基本集合(动态数组,链表, ...

  8. 大华java面试经验_大华面试(Java 基础)

    7-17 大华一面(java 基础) 1. 讲一下 java 和其他语言的区别,说下 java 的平台无关性,如何做到一次编译,到处运行 关于java 的特性,可以从 java 的优点来说. java ...

  9. 零基础学java web开发_从零基础学Java成为一个专业的java web 开发者

    原标题:从零基础学Java成为一个专业的java web 开发者 "我怎么才能成为一个Java Web开发者?"对于这个问题,答案其实并不简单.成为一个Java Web开发人员包括 ...

最新文章

  1. Graph Embedding方案之DeepWalk
  2. Xcode 修改系统的代码块样式 Code Snippet
  3. nginx源码分析之网络初始化
  4. Anti-Aliasing SSAA MSAA MLAA SRAA 简介
  5. 支持OpenStack,红帽将开源进行到底
  6. RabbitMQ 下载安装配置_集群高可用篇_02
  7. CSS3+JS 实现的便签应用
  8. 大众1.4t可以一直加92号汽油吗?有哪些需要注意的问题?
  9. SameSite Cookie
  10. PLC可编程控制实验装置及单片机综合实验台
  11. NEUQOJ 1999: 三角形or四边形?【搜索联通块/模拟】
  12. 移动硬盘安装操作系统以win7为例子
  13. Mono.Cecil 修改目标.NET的IL代码保存时报异常的处理。
  14. kindle paper white部分优化
  15. [Shiro教程] Shiro 教程基于SSM(SpringMVC + Spring + Mybatis)EHCache版本
  16. Eclips显示行号
  17. 视频教程-软件测试入门视频教程-软件测试
  18. 关于java的索引问题
  19. 深度学习100问之提高深度学习模型训练效果(调参经验)
  20. Smartphone 2.0 = Phone + Service

热门文章

  1. [转载] 利用python对csv文件进行简单的数据分析
  2. docker入门与部署微服务--学习笔记
  3. Java + selenium 元素定位(3)之By TagName
  4. C语言 scanf函数
  5. gentoo Portage使用
  6. nargout 【转】
  7. 第六章 线程的基础知识
  8. 数据结构上机实践第八周项目8-稀疏矩阵的三元组表示的实现及应用
  9. 数据结构笔记(十二)-- 定长顺序结构的实现
  10. 详说sizeof与strlen的区别与联系