Java 中的实例到底是什么?

发布于 2024-10-19 17:34:30 字数 50 浏览 2 评论 0原文

对象、实例和引用之间有什么区别?他们说他们必须为其应用程序创建一个实例?这意味着什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

家住魔仙堡 2024-10-26 17:34:30

对象和实例是同一事物

就我个人而言,我更喜欢在引用特定类型的特定对象时使用“实例”一词,例如“Foo 类型的实例”。但当谈论一般对象时,我会说“对象”而不是“实例”。

引用要么引用特定对象,要么它可以是空引用。


他们说他们必须为其应用程序创建一个实例。这是什么意思?

它们可能意味着您必须编写如下内容:

Foo foo = new 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 say that they have to create an instance to their application. What does it mean?

They probably mean you have to write something like this:

Foo foo = new Foo();

If you are unsure what type you should instantiate you should contact the developers of the application and ask for a more complete example.

谁与争疯 2024-10-26 17:34:30

“应用程序的实例”没有任何意义。

“对象”和“实例”是同一件事。有一个定义结构的“类”,以及该类的实例(通过new ClassName()获得)。例如,有类 Car,并且有具有不同属性的实例,例如里程、最大速度、马力、品牌等。

在 Java 上下文中,引用是一个变量* - 它是指向对象/实例的东西。例如,String s = null; - s 是一个引用,当前不引用任何实例,但可以引用 String 类的实例。

*Jon Skeet 注释了变量和引用之间的区别。看他的评论。调用方法时,Java 的工作方式有一个重要区别:按值传递。

s的值是一个参考。区分变量和值、对象和引用非常重要。

"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 class Car, 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 the String 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.

The value of s is a reference. It's very important to distinguish between variables and values, and objects and references.

度的依靠╰つ 2024-10-26 17:34:30

当您使用关键字 new(例如 JFrame j = new JFrame();)时,您正在创建类 JFrame 的实例。

new 运算符实例化一个
类通过为新的类分配内存
对象并返回对的引用
那段记忆。
注意:短语“实例化一个类”的含义与
“创建一个对象。”当你创建时
一个对象,你正在创建一个
因此,类的“实例”
“实例化”一个类。

看看这里
创建对象


Java编程的类型
语言分为两种
类别:原始类型
参考类型。
引用类型
class 类型、interface 类型和
数组类型。
还有一个特别的
null 类型。
一个对象是一个
动态创建的实例
class 类型或动态创建的
数组
引用的值
类型是对象的引用。

有关详细信息,请参阅类型、值和变量信息

When you use the keyword new for example JFrame j = new JFrame(); you are creating an instance of the class JFrame.

The new operator instantiates a
class by allocating memory for a new
object and returning a reference to
that memory.
Note: The phrase "instantiating a class" means the same thing as
"creating an object." When you create
an object, you are creating an
"instance" of a class, therefore
"instantiating" a class.

Take a look here
Creating Objects


The types of the Java programming
language are divided into two
categories: primitive types and
reference types.
The reference types
are class types, interface types, and
array types.
There is also a special
null type.
An object is a
dynamically created instance of a
class type or a dynamically created
array.
The values of a reference
type are references to objects.

Refer Types, Values, and Variables for more information

独留℉清风醉 2024-10-26 17:34:30

我认为对象=实例。引用是对象的“链接”。

Car c = new Car();

变量 c 存储对 Car 类型的对象的引用。

I think that Object = Instance. Reference is a "link" to an Object.

Car c = new Car();

variable c stores a reference to an object of type Car.

清风无影 2024-10-26 17:34:30
Computer c= new Computer()

这里从 Computer 类创建了一个对象。名为 c 的引用允许程序员访问该对象。

Computer c= new Computer()

Here an object is created from the Computer class. A reference named c allows the programmer to access the object.

我的奇迹 2024-10-26 17:34:30

主要区别是当您说 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.

梦里的微风 2024-10-26 17:34:30

基本上,对象和实例是可以互换使用的两个词。
类是对象的模板,对象是类的实例。

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.

过去的过去 2024-10-26 17:34:30

“创建一个类的实例”怎么样,“你正在获取一个类并创建该类的一个新变量,该变量将根据更改的输入而更改”

库中名为 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!

飘然心甜 2024-10-26 17:34:30

实例的字面意思是“某事物的一个例子或单个事件”。这与 Java 术语中的“实例”非常接近。

Java遵循动态加载,不像C语言那样在运行时将所有代码复制到RAM中。让我们用一个例子来捕捉这一点。

   class A
    {
    int x=0;
    public static void main(String [] args)    
     {
    int y=0;
    y=y+1;
    x=x+1;
     }   
    
    }

让我们编译并运行这段代码。

步骤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.

   class A
    {
    int x=0;
    public static void main(String [] args)    
     {
    int y=0;
    y=y+1;
    x=x+1;
     }   
    
    }

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 "

表情可笑 2024-10-26 17:34:30

对象,也称为实例,是具有相关功能的程序的独立元素和数据。大多数情况下,您仅使用类来创建实例,然后使用这些实例。

-定义取自《Sams Teach Yourself Java in 21 days》一书。

假设您有 2 个类,public class MainClasspublic class Class_2,并且您希望在 MainClass 中创建Class_2 的实例

这是一个非常简单和基本的方法:

public MainClass()      /*******this is the constructor of MainClass*******/

{

 Class_2 nameyouwant = new Class_2();

}

我希望这会有所帮助!

Objects, which are also called instances, are self-contained elements of a program with related features and data. For the most part, you use the class merely to create instances and then work with those instances.

-Definition taken from the book "Sams Teach Yourself Java in 21 days".

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:

public MainClass()      /*******this is the constructor of MainClass*******/

{

 Class_2 nameyouwant = new Class_2();

}

I hope this helps!

深海蓝天 2024-10-26 17:34:30

实例变量:它必须附加到对象上。该类中的实例变量只能在实例化该类后才能使用

public class Test{
   static int a = 13;
   int b = 14;


   public static void main(String[] args){
       int d = new Test().b;
       System.out.println(d);
   }
}

Instance variable: It must be attached to the object. Instance variables in this class can only be used after instantiating the class

public class Test{
   static int a = 13;
   int b = 14;


   public static void main(String[] args){
       int d = new Test().b;
       System.out.println(d);
   }
}
仅此而已 2024-10-26 17:34:30

实例=内存在运行时为称为实例的任何对象分配内存

对象=内存在运行时为称为对象的类分配

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文