简体   繁体   English

Java 构造函数和初始化类变量

[英]Java Constructors and initializing class variables

In Java, does the constructor of a class create an instance of that class?在 Java 中,类的构造函数是否会创建该类的实例? And if it does, does it also initialize the variables of that class?如果是,它是否也会初始化该类的变量?

Constructor doesn’t create the instance of the Class.

Instance creation is done using either:

  1.Using Class.forName()

  2.ClassLoader loadClass()

  3.Using clone()

  4.Deserialization

  5.Using reflection

  6.new keyword


Constructor in java is a special type of method that is used to initialize the object.

Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Rules for creating java constructor

There are basically two rules defined for the constructor.

      1.Constructor name must be same as its class name

      2.Constructor must have no explicit return type

Types of java constructors

 There are two types of constructors:

     1.Default constructor (no-arg constructor)

     2.Parameterized constructor

Constructors don't create objects.构造函数不创建对象。 They merely initialize objects(and their data members) once they are created using parameters(when provided) or to default values.一旦使用参数(提供时)或默认值创建对象,它们仅初始化对象(及其数据成员)。

When you create an instance of the class using new operator, the constructor of the class is called so as to initialize the instance variables.当您使用 new 运算符创建类的实例时,会调用类的构造函数以初始化实例变量。 If the constructor defined is default, then instance variables have to be assigned to the newly created object explicitly.如果定义的构造函数是默认的,那么实例变量必须显式分配给新创建的对象。 However when you override a constructor using fields, then the instance variables for that newly created object are assigned during object creation.但是,当您使用字段覆盖构造函数时,会在对象创建期间分配该新创建对象的实例变量。

I would love to explain this in a very simple language.我很想用一种非常简单的语言来解释这一点。 In the real-world to build something, we need two things first is its prototype/model, and the second is someone who can create it based on that prototype.在现实世界中,我们需要两个东西,第一是它的原型/模型,第二是可以基于该原型创建它的人。 A very relevant simple example is to build a house, you first need its blueprint(map), then a constructor who can build it based on that blueprint.一个非常相关的简单例子是建造一座房子,你首先需要它的蓝图(地图),然后是一个可以基于该蓝图建造它的构造函数。 So, similarly in the programming language所以,同样在编程语言中

Object : A real-world entity for which we create a class. Object :我们为其创建类的真实世界实体。

Class : A class describes the "blueprint" of the objects that are made out of it (are instances of it).:类描述了由它构成的对象的“蓝图”(是它的实例)。

  1. For software development, we first have to think about the objects(any real-world entity), then we create a class (blueprint) for it, which contains its attributes.对于软件开发,我们首先要考虑对象(任何现实世界的实体),然后为它创建一个类(蓝图),其中包含它的属性。
  2. After creating a class when we need to create one or more objects based on it, for this, we need a constructor to build it.创建一个类之后,当我们需要基于它创建一个或多个对象时,为此,我们需要一个构造函数来构建它。
  3. Whenever we create a new object, we have to use new keyword, and it tells the constructor to create the object.每当我们创建一个新对象时,我们必须使用new关键字,它告诉构造函数创建对象。

When you are initialing variables in a class they are just part of the blueprint, based on that, the object will be created.当您在类中初始化变量时,它们只是蓝图的一部分,基于此,将创建对象。 So, without a constructor, you cannot create new objects, but there are some exceptional cases and tricks where you can create them without calling constructors.因此,如果没有构造函数,您将无法创建新对象,但有一些例外情况和技巧,您可以在不调用构造函数的情况下创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM