Calling Another Constructor

if the first statement of a constructor has the form this(...), then the constructor calls another constructor of the same class. For example,

public Employee(double s)
{
this("Employee #" + nextId, s);
nextId++;
} When you call `new Employee(60000), the `Employee(double)` constructor calls the `Employee(String, double)` constructor.

import java.util.*;

/*

  • This program demonstrates object construction.

    */

public class ConstructorTest

{

public static void main(String[] args)

{

// fill the staff array with three Employee objects

Employee[] staff = new Employee[3];

staff[0] = new Employee("Harry", 40000);

staff[1] = new Employee(60000);

staff[2] = new Employee();

    //print out inofrmation about all Employee objects
for(Employee anEmployee : staff)
{
System.out.println("name =" + anEmployee.getName() + ", id =" + anEmployee.getId() + ", salary="
+ anEmployee.getSalary());
} }

}

class Employee

{

private static int nextId;

private int id;
private String name = ""; //instance field initialization
private double salary; //static initialization block
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
} // object initialization block {
id = nextId;
nextId++;
} // three overload constructors public Employee(String n, double s)
{
name = n;
salary = s;
System.out.println("Constructor: Employee(String n, double s) is called.");
} public Employee(double s)
{
// calls the Employee(String, doulbe)constructor
this("Employee #" + nextId, s); } // the default constructor
public Employee()
{
// name initialization to "" --see above
// salary not explicitly set -- initialized to 0
// id initialized in initialization block
System.out.println("The default constructor Employee() is called.");
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public int getId()
{
return id;
}

}

输出结果:

Constructor: Employee(String n, double s) is called.
Constructor: Employee(String n, double s) is called.
The default constructor Employee() is called.
Disconnected from the target VM, address: '127.0.0.1:36707', transport: 'socket'
name =Harry, id =257, salary=40000.0
name =Employee #258, id =4283, salary=60000.0
name =, id =2113, salary=0.0 Process finished with exit code 0

理解Java构造器中的"this"的更多相关文章

  1. 深入理解Java虚拟机--中

    深入理解Java虚拟机--中 第6章 类文件结构 6.2 无关性的基石 无关性的基石:有许多可以运行在各种不同平台上的虚拟机,这些虚拟机都可以载入和执行同一种平台无关的字节码(ByteCode),从而 ...

  2. 轻松理解 Java开发中的依赖注入(DI)和控制反转(IOC)

    前言 关于这个话题, 网上有很多文章,这里, 我希望通过最简单的话语与大家分享. 依赖注入和控制反转两个概念让很多初学这迷惑, 觉得玄之又玄,高深莫测. 这里想先说明两点: 依赖注入和控制反转不是高级 ...

  3. 垃圾回收相关(深入理解Java虚拟机中的内容)

    程序计数器.虚拟机栈和本地方法栈这三个区域属于线程私有的,只存在于线程的生命周期内,线程结束之后也会消失,因此不需要对这三个区域进行垃圾回收.垃圾回收主要是针对 Java 堆和方法区进行. 判断一个对 ...

  4. 理解Java序列化中的SerialVersionUid

    一.前言 SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容.如果在新版本中这个值修改了,新版本就不兼容旧版本,反序列化时会抛出InvalidClass ...

  5. 理解Java虚拟机中的栈、堆、堆栈

    JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 栈区: 每个线程包含一个栈区,栈中只保存方法中(不包括对象的成员变量)的基础数据类型和自定义对象的引用(不 ...

  6. 深入理解Java中的锁

    转载:https://www.jianshu.com/p/2eb5ad8da4dc Java中的锁 常见的锁有synchronized.volatile.偏向锁.轻量级锁.重量级锁 1.synchro ...

  7. 转:理解Java泛型

    JDK 5.0 中增加的泛型类型,是 Java 语言中类型安全的一次重要改进.但是,对于初次使用泛型类型的用户来说,泛型的某些方面看起来可能不容易明白,甚至非常奇怪.在本月的“Java 理论和实践”中 ...

  8. 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因

    声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...

  9. 深入理解Java中的不可变对象

    深入理解Java中的不可变对象 不可变对象想必大部分朋友都不陌生,大家在平时写代码的过程中100%会使用到不可变对象,比如最常见的String对象.包装器对象等,那么到底为何Java语言要这么设计,真 ...

随机推荐

  1. R python在无图形用户界面时保存图片

    在用python的matplotlib,和R中自带的作图,如果想保存图片时,当你有图形用户界面时是没有问题的,但是当没有图形用户界面时,会报错: 在R中,解决办法: https://blog.csdn ...

  2. 专人写接口+模型,专人写业务逻辑---interface_model -- business logical

    专人写接口+模型,专人写业务逻辑---interface_model -- business logical 0-控制台脚本重构为“面向接口编程”:1-仓库类通过__constru方法,来实现一处实例 ...

  3. import的项目结构不对

    问题如下,在我们新导入一个maven项目时,碰到这样的目录结构,总有点别扭,而且在运行Tomcat的时候,突然发现build i选项下面少了两个我们经常使用的两个选项 window  --Perspe ...

  4. Linux 两台服务器之间传递文件

    参考: https://www.cnblogs.com/clovershell/p/9870603.html linux采用scp命令拷贝文件到本地,拷贝本地文件到远程服务器   // 假设远程服务器 ...

  5. 16/7/11_PHP-文件系统

    读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $content = file_get_contents ...

  6. MAC-安装套件管理工具Homebrew

    前言 Homebrew是一款Mac OS下的套件管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能. Homebrew安装 1,Homebrew官网获取安装指令,官网地址:https://br ...

  7. LeetCode 112. Path Sum 动态演示

    给一个目标值,判断一棵树从根到叶子是否至少有一条路径加起来的和等于目标值 比较典型的深度优先算法. 引入一个全局变量bResult, 一旦找到一条,就不再搜索其他的了. class Solution ...

  8. Android深度探索-卷1第八章心得体会

    本章介绍了如何将Linux驱动分成多个实现文件和Linux常用的代码重用方式还有些强行卸载Linux驱动的方法 开发一个Linux驱动,可能会在init.exit等函数中发生错误导致Linux驱动安装 ...

  9. Java8数据流

    流/Stream是在JAVA8中引入的一个抽象,可以处理类似SQL语句声明数据. 例如,考虑下面的SQL语句. SELECT max(salary),employee_id,employee_name ...

  10. ERROR [localhost-startStop-1] - Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/ap

    ERROR [localhost-startStop-1] - Context initialization failed org.springframework.beans.factory.Bean ...