Java 中的实例到底是什么?
对象、实例和引用之间有什么区别?他们说他们必须为其应用程序创建一个实例?这意味着什么?
What is the difference between an object, instance, and reference? They say that they have to create an instance to their application? What does that mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对象和实例是同一事物。
就我个人而言,我更喜欢在引用特定类型的特定对象时使用“实例”一词,例如“Foo 类型的实例”。但当谈论一般对象时,我会说“对象”而不是“实例”。
引用要么引用特定对象,要么它可以是空引用。
它们可能意味着您必须编写如下内容:
如果您不确定应该实例化什么类型,您应该联系应用程序的开发人员并要求提供更完整的示例。
An object and an instance are the same thing.
Personally I prefer to use the word "instance" when referring to a specific object of a specific type, for example "an instance of type Foo". But when talking about objects in general I would say "objects" rather than "instances".
A reference either refers to a specific object or else it can be a null reference.
They probably mean you have to write something like this:
If you are unsure what type you should instantiate you should contact the developers of the application and ask for a more complete example.
“应用程序的实例”没有任何意义。
“对象”和“实例”是同一件事。有一个定义结构的“类”,以及该类的实例(通过
new ClassName()
获得)。例如,有类Car
,并且有具有不同属性的实例,例如里程、最大速度、马力、品牌等。在 Java 上下文中,引用是一个变量* - 它是指向对象/实例的东西。例如,
String s = null;
-s
是一个引用,当前不引用任何实例,但可以引用String
类的实例。*Jon Skeet 注释了变量和引用之间的区别。看他的评论。调用方法时,Java 的工作方式有一个重要区别:按值传递。
"instance to an application" means nothing.
"object" and "instance" are the same thing. There is a "class" that defines structure, and instances of that class (obtained with
new ClassName()
). For example there is the classCar
, and there are instance with different properties like mileage, max speed, horse-power, brand, etc.Reference is, in the Java context, a variable* - it is something pointing to an object/instance. For example,
String s = null;
-s
is a reference, that currently references no instance, but can reference an instance of theString
class.*Jon Skeet made a note about the difference between a variable and a reference. See his comment. It is an important distinction about how Java works when you invoke a method - pass-by-value.
当您使用关键字
new
(例如JFrame j = new JFrame();
)时,您正在创建类JFrame
的实例。看看这里
创建对象
有关详细信息,请参阅类型、值和变量信息
When you use the keyword
new
for exampleJFrame j = new JFrame();
you are creating an instance of the classJFrame
.Take a look here
Creating Objects
Refer Types, Values, and Variables for more information
我认为对象=实例。引用是对象的“链接”。
变量 c 存储对 Car 类型的对象的引用。
I think that Object = Instance. Reference is a "link" to an Object.
variable c stores a reference to an object of type Car.
这里从
Computer
类创建了一个对象。名为 c 的引用允许程序员访问该对象。Here an object is created from the
Computer
class. A reference named c allows the programmer to access the object.主要区别是当您说 ClassName obj = null; 时您只是为该类创建一个对象。它不是该类的实例。
该语句只会为静态成员变量分配内存,而不为普通成员变量分配内存。
但是当你说 ClassName obj = new ClassName();您正在创建该类的一个实例。该语句将为所有成员变量分配内存。
The main differnece is when you say ClassName obj = null; you are just creating an object for that class. It's not an instance of that class.
This statement will just allot memory for the static meber variables, not for the normal member variables.
But when you say ClassName obj = new ClassName(); you are creating an instance of the class. This staement will allot memory all member variables.
基本上,对象和实例是可以互换使用的两个词。
类是对象的模板,对象是类的实例。
basically object and instance are the two words used interchangeably.
A class is template for an object and an object is an instance of a class.
“创建一个类的实例”怎么样,“你正在获取一个类并创建该类的一个新变量,该变量将根据更改的输入而更改”
库中名为 Nacho
变量 Libre 的类来保存该“实例”将更改
Nacho Libre = new Nacho(变量、扫描仪输入或此处的任何内容,这是接受更改的地方,然后将值放在等号左侧的“Libre”中(您知道“Nacho Libre = new Nacho(Scanner.in)”“Nacho Libre”位于 = 的左侧(这不是技术谈话,这是我的解释方式)
我认为这比说“类型实例”或“类实例”更好。真正的重点是它只需要详细说明......“类型或类的实例”对于初学者来说不够好......哇,它就像绕口令,你的大脑无法专注于绕口令很好……那个“实例”这个词非常烦人,光是它的声音就让我发疯……它需要更多的细节……它需要更好地分解。我不得不用谷歌搜索“实例”意味着什么,只是为了弄清楚我的方位......尝试对你的奶奶说“类实例”......哎呀!
"creating an instance of a class" how about, "you are taking a class and making a new variable of that class that WILL change depending on an input that changes"
Class in the library called Nacho
variable Libre to hold the "instance" that will change
Nacho Libre = new Nacho(Variable, Scanner Input, or whatever goes here, This is the place that accepts the changes then puts the value in "Libre" on the left side of the equals sign (you know "Nacho Libre = new Nacho(Scanner.in)" "Nacho Libre" is on the left of the = (that's not tech talk, that's my way of explaining it)
I think that is better than saying "instance of type" or "instance of class". Really the point is it just needs to be detailed out more.... "instance of type or class" is not good enough for the beginner..... wow, its like a tongue twister and your brain cannot focus on tongue twisters very well.... that "instance" word is very annoying and the mere sound of it drives me nuts.... it begs for more detail.....it begs to be broken down better. I had to google what "instance" meant just to get my bearings straight..... try saying "instance of class" to your grandma.... yikes!
实例的字面意思是“某事物的一个例子或单个事件”。这与 Java 术语中的“实例”非常接近。
Java遵循动态加载,不像C语言那样在运行时将所有代码复制到RAM中。让我们用一个例子来捕捉这一点。
让我们编译并运行这段代码。
步骤1:javac A.class(生成.class文件,它是字节码)
步骤2:java A(.class文件转换为可执行代码)
在步骤2中,main方法和静态元素被加载到RAM中以便执行。在上面的场景中,直到 y=y+1 行才出现问题。但是每当执行 x=x+1 时,就会抛出运行时错误,因为JVM 不知道在 main 方法之外声明的 x 是什么(非静态)。
因此,如果通过某种方式,.class 文件的内容可以在内存中供 CPU 执行,则不再有问题。
这是通过创建对象和关键字 NEW 来完成此工作。
“运行时在 RAM 中为硬盘内容(此处为 .class 文件)保留内存的概念称为实例”
The Literal meaning of instance is "an example or single occurrence of something." which is very closer to the Instance in Java terminology.
Java follows dynamic loading, which is not like C language where the all code is copied into the RAM at runtime. Lets capture this with an example.
Let us compile and run this code.
step 1: javac A.class (.class file is generated which is byte code)
step 2: java A (.class file is converted into executable code)
During the step 2,The main method and the static elements are loaded into the RAM for execution. In the above scenario, No issue until the line y=y+1. But whenever x=x+1 is executed, the run time error will be thrown as the JVM does not know what the x is which is declared outside the main method(non-static).
So If by some means the content of .class file is available in the memory for CPU to execute, there is no more issue.
This is done through creating the Object and the keyword NEW does this Job.
"The concept of reserving memory in the RAM for the contents of hard disk (here .class file) at runtime is called Instance "
假设您有 2 个类,public class MainClass 和 public class Class_2,并且您希望在 MainClass 中创建Class_2 的实例。
这是一个非常简单和基本的方法:
我希望这会有所帮助!
Say you have 2 Classes, public class MainClass and public class Class_2 and you want to make an instance of Class_2 in MainClass.
This is a very simple and basic way to do it:
I hope this helps!
实例变量:它必须附加到对象上。该类中的实例变量只能在实例化该类后才能使用
Instance variable: It must be attached to the object. Instance variables in this class can only be used after instantiating the class
实例=内存在运行时为称为实例的任何对象分配内存
对象=内存在运行时为称为对象的类分配
Instance = memory is allocated at run time for anything that is called instance
Object = memory is allocated at run time for class that is called object