JAVA中实例化的确切含义是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44315657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the exact meaning of instantiate in JAVA
提问by Rajat Singh
I'am a newbie in JAVA and this came across this word called. "A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiatethis class inside the method". The word in bold. Can anyone please help me out with this one.I know it's embarrassing and i should've researched more but I just cannot understand. Thanks.
我是 JAVA 的新手,遇到了这个词叫做。“在方法内部即创建的类在java中称为局部内部类。如果要调用局部内部类的方法,则必须在方法内部实例化此类”。粗体字。任何人都可以帮我解决这个问题。我知道这很尴尬,我应该进行更多研究,但我无法理解。谢谢。
采纳答案by Blasanka
First of all Declaring mean:
首先声明的意思是:
ClassName obj;
Simple meaning of instantiate is creating an object from class.
实例化的简单含义是从类创建一个对象。
ClassName obj = new ClassName();
What is a object?
什么是对象?
- An instance of a class. From one class we can create many instances.
- They are the basic runtime entities in in our program.
- They may also represent user-defined data types such as lists and vectors.
- Any programming problem is analyzed in terms of objects and nature of communication between them.
- 一个类的实例。从一个类中,我们可以创建许多实例。
- 它们是我们程序中的基本运行时实体。
- 它们还可以表示用户定义的数据类型,例如列表和向量。
- 任何编程问题都是根据对象和它们之间通信的性质来分析的。
As a example:
例如:
//Define a reference(a variable) which can hold a `Person` obect.
Person p;
//Create a Person object(instantiate).
//new - use to allocate memory space for the new object
p = new Person();
What is a nested class?
什么是嵌套类?
A class that defined inside a class is called nested class. There 2 categories of nested classes.
在类内部定义的类称为嵌套类。有 2 类嵌套类。
Inner class:
内部类:
- Inner class can only be accessed by the outer class. Not by any other class.
- Inner class is a member of outer class.
- Outer class can access inner class without importing.
- Inner class can access any attribute or a method belong to outer directly.
- Outer class cannot access directly to a inner class.
- 内部类只能被外部类访问。不是任何其他班级。
- 内部类是外部类的成员。
- 外部类无需导入即可访问内部类。
- 内部类可以直接访问属于外部的任何属性或方法。
- 外部类不能直接访问内部类。
Example for a inner class:
内部类示例:
class Outer{
int i = 10;
void main(){
//instantiate inner class.
Inner in = new Inner();
in.show();
}
class Inner{
void show(){
System.out.print(i);
}
}
}
What is a local class?
什么是本地类?
Which are classes that are defined in a block.
哪些是在块中定义的类。
Example:
例子:
public class{
int i = 10;
public main(){
class A{
void show(){
System.out.println(i);
}
}
//inside the method instantiate local class.
A obj = new obj();
obj.show();
}
//outside the main() -block(method)
//inside another method instantiate local class.
public test(){
A obj = new A();
obj.show();
}
}
回答by nhouser9
To instantiate a class means to create an instance of the class. In other words, if you have a class like this:
实例化一个类意味着创建一个类的实例。换句话说,如果你有一个这样的类:
public class Dog {
public void bark() {
System.out.println("woof");
}
}
You would instantiate it like this:
你会像这样实例化它:
Dog myDog = new Dog();
Instantiating is when you use the new
keyword to actually create an object of your class.
实例化是当您使用new
关键字实际创建类的对象时。
回答by Vitaliy Moskalyuk
create an instance of the class by using "new" word
使用“新”字创建类的实例
for example Car car = new Car();
例如 Car car = new Car();
回答by Ivan Pronin
Instantiate == create an instance == create an object of a class.
实例化 == 创建一个实例 == 创建一个类的对象。
回答by Anzurio
Instantiate is creating an instance of a class. I reckon this is not helpful without knowing what an instance is.
Instantiate 正在创建一个类的实例。我认为这在不知道实例是什么的情况下没有帮助。
Let's say you have a class definition like:
假设您有一个类定义,例如:
public class Person
{
private String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
You make an instance of this class my calling its constructor and using the keyword new
:
你让这个类的一个实例调用它的构造函数并使用关键字new
:
Person p = new Person("Hugh Laurie");
An instance of a class is a place in memory that contains the state (e.g., Person::name
) of a given object which used as a template a class definition.
类的实例是内存中的一个地方,它包含Person::name
用作类定义模板的给定对象的状态(例如,)。
I want to further expand upon:
我想进一步扩展:
If you want to invoke the methods of local inner class you must instantiate this class
如果你想调用本地内部类的方法,你必须实例化这个类
What this means is that, you need to have instantiated that class in order to use the above's example getName()
method. After all, it is trying to access the state (name
) of a given object in memory; without that object, there is no state.
这意味着,您需要实例化该类才能使用上面的示例getName()
方法。毕竟,它试图访问name
内存中给定对象的状态 ( );没有那个对象,就没有状态。
回答by Novaterata
Instantiate in Java means to call a constructorof a Class which creates an an instance or object, of the type of that Class. Instantiation allocates the initial memoryfor the object and returns a reference. An instance is required by non-static methods as they may operate on the non-static fields created by the constructor.
在 Java 中实例化意味着调用类的构造函数,该构造函数创建该类类型的实例或对象。实例化为对象分配初始内存并返回一个引用。非静态方法需要一个实例,因为它们可能对构造函数创建的非静态字段进行操作。
Static methods don't need an instance and should not be stateful, i.e. should not rely on changing data. They are essentially free functions that are associated with the type and not a particular instance. When you want to work with changing data, encapsulating that data as member fields that are operated on by instance methodsis the way to go.
静态方法不需要实例,也不应该是有状态的,即不应该依赖于不断变化的数据。它们本质上是与类型而不是特定实例相关联的自由函数。当您想要处理不断变化的数据时,将这些数据封装为由实例方法操作的成员字段是一种可行的方法。
For example, a Car class might have static numbeOfWheels() that always returns 4, but an instance numberOfFlatTires() that might return 0-4 depending on the state of that particular Car.
例如,一个 Car 类可能有静态的 numbeOfWheels(),它总是返回 4,但一个实例 numberOfFlatTires() 可能返回 0-4,具体取决于该特定 Car 的状态。
Inner classes are no different and the only difference between a static and non-static inner class is that the non-static can use the parent instance's members. This can be used to reduce complexity. You might have a looping operation that has a common parameter for the list and an individual parameter for the items. You could use a non-static inner class to encapsulate the operations on the item while referring to the common parameter in the parent class.
内部类没有什么不同,静态和非静态内部类之间的唯一区别是非静态可以使用父实例的成员。这可用于降低复杂性。您可能有一个循环操作,该操作具有列表的公共参数和项目的单独参数。您可以使用非静态内部类来封装对项目的操作,同时引用父类中的公共参数。
Enums are special in that each value is a single instance of a single type that all extend from a common abstract base class defined in the Enum class body. The Enum value is instantiated the first time it's used, but there will only ever be one instance per value.
枚举的特殊之处在于,每个值都是单个类型的单个实例,它们都从枚举类主体中定义的公共抽象基类扩展而来。Enum 值在第一次使用时被实例化,但每个值永远只有一个实例。
回答by majid khan
when you create an instance of a class or simply called as an object
当您创建类的实例或简单地称为对象时
for ex: Bike abc = new Bike();
例如:自行车 abc = new Bike();
as soon as you create this object using the new keyword, a new block of memory is created and object "abc" will now be pointing to that new block of memory, this is called as instantiation in java.
一旦您使用 new 关键字创建此对象,就会创建一个新的内存块,并且对象“abc”现在将指向该新内存块,这在 java 中称为实例化。