初始化类和实例化对象之间的区别? [英] Difference between initializing a class and instantiating an object?

查看:484
本文介绍了初始化类和实例化对象之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过搜索引擎搜索此问题,但可以找到一个主题,该主题解释了初始化类和实例化对象之间的区别.

I tried searching for this question through the search engine but could find a topic that explained the difference between initializing a class and instantiating an object.

有人可以解释他们之间的区别吗?

Could someone explain how they differ?

推荐答案

与该主题相关的三种术语是:声明,初始化和实例化.

There are three pieces of terminology associated with this topic: declaration, initialization and instantiation.

从背面到正面工作.

实例

这是为对象分配内存的时间.这就是new关键字的作用.从new关键字返回对创建的对象的引用.

This is when memory is allocated for an object. This is what the new keyword is doing. A reference to the object that was created is returned from the new keyword.

初始化

这是将值放入分配的内存中的时间.这就是使用<时类的构造器的作用. c0>关键字.

This is when values are put into the memory that was allocated. This is what the Constructor of a class does when using the new keyword.

还必须通过将对内存中某个对象的引用传递给它来初始化变量.

A variable must also be initialized by having the reference to some object in memory passed to it.

声明

这是当您向程序声明将存在某种特定类型的对象以及该对象的名称时.

This is when you state to the program that there will be an object of a certain type existing and what the name of that object will be.

同一行上的初始化和实例化示例

SomeClass s; // Declaration
s = new SomeClass(); // Instantiates and initializes the memory and initializes the variable 's'

在与内存不同的行上初始化变量的示例

void someFunction(SomeClass other) {
    SomeClass s; // Declaration
    s = other; // Initializes the variable 's' but memory for variable other was set somewhere else
}

我也强烈建议阅读本文关于Java如何处理传递变量的性质.

I would also highly recommend reading this article on the nature of how Java handles passing variables.

这篇关于初始化类和实例化对象之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆