知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

      • Theory:Introduction OOP
      • Theory:Defining classes
      • Theory:Instance methods
      • Thoery:Constructor
        • Using constructors
        • Keyword this
        • Default and no-argument constructor
        • To sum up
      • Theory:Package
      • Theory:Access modifiers
      • Theory:Getters and setters
      • Theory:Inheritance
      • Theory:Protected modifier
      • Theory:Referencing subclass objects
      • Theory:The Object class
      • Theory:Objects
      • Theory:Interface
      • Theory:Multiple constructors
      • Theory:The keyword super
      • Theory:Static members
      • Theory:Hiding and overriding
      • Theory:Polymorphism
      • Theory:Abstract class
      • Theory:Abstract class vs interface
      • Theory:Anonymous classes
      • Theory:Anonymous classes properties
    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java OOP
Jim
2022-05-06
目录

Thoery:Constructor

Constructors are special methods that initialize a new object of the class. A constructor of a class is invoked when an instance is created using the keyword new.

A constructor is different from other methods in that:

  • it has the same name as the class that contains it;
  • it has no return type (not even void).

Constructors initialize instances (objects) of the class. They set values to the fields when the object is created. Also, constructors can take parameters for initializing fields by the given values.

# Using constructors

Here is a class named Patient. An object of the class has a name, an age, and a height. The class has a three-argument constructor to initialize objects with specific values.

class Patient {

    String name;
    int age;
    float height;

    public Patient(String name, int age, float height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

Let's go further and create some instances of the class using the constructor we've written:

Patient patient1 = new Patient("Heinrich", 40, 182.0f);
Patient patient2 = new Patient("Mary", 33, 171.5f);
1
2

Now we have two patients, Heinrich and Mary, with the same fields, but the values of those fields are different.

# Keyword this

In the example above, Patient constructor takes three parameters:

this.name = name;
this.age = age;
this.height = height;
1
2
3

To initialize the fields, the keyword this is used, which is a reference to the current object. Usually, the keyword this is used when an instance variable and a constructor or a method variable share the same name. This keyword helps to disambiguate these situations.

If you write something like name = name, it means that you're assigning the name variable to itself, which, of course, doesn't make any sense. Frankly speaking, you may distinguish two objects simply by assigning another name to the variable, like name = newName. It is not prohibited, but it is considered bad practice since these variables point to the same thing. These are the reasons why the keyword thisis extremely useful with constructors, fields, and methods. The absence of extra variables makes the code look clearer and less overloaded.

# Default and no-argument constructor

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

class Patient {

    String name;
    int age;
    float height;
}
1
2
3
4
5
6

We can create an instance of the class Patient using the no-argument default constructor:

Patient patient = new Patient();
1

In this case, all fields will be filled with the default values of their types.

If you define a specific constructor, the default constructor will not be created.

We can also define a constructor without any arguments, but use it to set default values for fields of a class. For example, we can initialize name with "Unknown":

class Patient {

    String name;
    int age;
    float height;

    public Patient() {
        this.name = "Unknown";
    }
}
1
2
3
4
5
6
7
8
9
10

Such no-argument constructors are useful in cases when any default value is better than null.

# To sum up

  • Any Java class has a constructor to initialize objects;
  • A constructor has the same name as the class containing it;
  • A constructor has no return type, not even void;
  • If a class has no explicit constructors, the Java compiler automatically provides a default no-argument constructor;
  • If we want to introduce new variables to denote the same thing, make the code clearer and less loaded with extra variables, the keyword this is used.
编辑 (opens new window)
#Java#OOP
上次更新: 2022/09/26, 16:55:15
Theory:Instance methods
Theory:Package

← Theory:Instance methods Theory:Package→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式