java 在Java中创建静态对象的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41611228/
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 purpose of creating static object in Java?
提问by Ajay Khetan
class Demo {
void show() {
System.out.println("i am in show method of super class");
}
}
class Flavor1Demo {
// An anonymous class with Demo as base class
static Demo d = new Demo() {
void show() {
super.show();
System.out.println("i am in Flavor1Demo class");
}
};
public static void main(String[] args){
d.show();
}
}
In the above code, I do not understand the use of creating object d of Demo class with static keyword preceding it. If I eliminate the static keyword, it shows an error. Actually, I was going through anonymous inner class concept and got stuck here. Need help.... Can anyone please explain it?
在上面的代码中,我不明白创建 Demo 类的对象 d 的用途是在它前面加上 static 关键字。如果我消除了 static 关键字,它会显示一个错误。实际上,我正在经历匿名内部类的概念并被困在这里。需要帮助.... 谁能解释一下?
回答by Sourav Purakayastha
The static keyword in Java means that the variable or function is shared between all instances of that class, not the actual objects themselves.
Java 中的 static 关键字意味着变量或函数在该类的所有实例之间共享,而不是实际对象本身。
In your case, you try to access a resource in a static
method,
在您的情况下,您尝试在static
方法中访问资源,
public static void main(String[] args)
Thus anything we access here without creating an instance of the class Flavor1Demo
has to be a static
resource.
因此,我们在不创建类的实例的情况下访问的任何内容都Flavor1Demo
必须是static
资源。
If you want to remove the static
keyword from Demo
class, your code should look like:
如果您想static
从Demo
类中删除关键字,您的代码应如下所示:
class Flavor1Demo {
// An anonymous class with Demo as base class
Demo d = new Demo() {
void show() {
super.show();
System.out.println("i am in Flavor1Demo class");
}
};
public static void main(String[] args) {
Flavor1Demo flavor1Demo = new Flavor1Demo();
flavor1Demo.d.show();
}
}
Here you see, we have created an instance of Flavor1Demo
and then get the non-static
resource d
The above code wont complain of compilation errors.
在这里你看到,我们已经创建了一个实例,Flavor1Demo
然后获取non-static
资源d
上面的代码不会抱怨编译错误。
Hope it helps!
希望能帮助到你!
回答by Yousaf
You get an error by removing static
keyword from static Demo d = new Demo()
because you are using that object d
of class Demo
in main
method which is static
. When you remove static
keyword from static Demo d = new Demo()
, you are making object d
of your Demo
class non-static
and non-static
object cannot be referenced from a static
context.
删除static
关键字 from会导致错误,static Demo d = new Demo()
因为您在方法中使用d
了该类的对象。当您删除的关键字,你正在对象你的类和对象不能从引用的上下文。Demo
main
static
static
static Demo d = new Demo()
d
Demo
non-static
non-static
static
If you remove d.show();
from main
method and also remove static
keyword from static Demo d = new Demo()
, you won't get the error.
如果您d.show();
从main
方法中删除并从 中删除static
关键字static Demo d = new Demo()
,您将不会收到错误消息。
Now if you want to call the show
method of Demo
class, you would have to create an object of your Demo
class inside main
method.
现在,如果要调用类的show
方法,则Demo
必须Demo
在main
方法内部创建类的对象。
public static void main(String[] args){
Demo d = new Demo();
d.show();
}
回答by Tomasz Mularczyk
Thats because you try to use d
that belongs to object in static method.
那是因为您尝试d
在静态方法中使用属于对象的对象。
You would then have to create that object in main method.
然后,您必须在 main 方法中创建该对象。
static belongs to class, not object itself.
静态属于类,而不是对象本身。
I want to know the difference between static method and non-static method
回答by AxelH
That depends on the context you are in.
这取决于你所处的环境。
The main(String[])
methods is a static methods.
该main(String[])
方法是静态方法。
To stay simple, that means it doesn't exist in an instance of Flavor1Demo
, there is no this
here. If you set d
as non-static, it will only exist in an instance of Flavor1Demo
so it can't be access from the main
unless you build a instance first (new Flavor1Demo().d.show();
为了简单起见,这意味着它不存在于 的实例中Flavor1Demo
,this
此处不存在。如果您设置d
为非静态,它将仅存在于实例中,Flavor1Demo
因此main
除非您先构建实例(新Flavor1Demo().d.show();
But a static variable will be link to the class an not an instance, Meaning you can access it from a static context. In you case, the main method.
但是静态变量将链接到类而不是实例,这意味着您可以从静态上下文访问它。在你的情况下,主要方法。
回答by Amarthya Sai
In order to access methods or variables in main class without creating object in it,here we are defining anonymous inner class where we create object of static type so that its directly accessed from main class without creating the object.
为了在不创建对象的情况下访问主类中的方法或变量,这里我们定义了匿名内部类,在该类中我们创建了静态类型的对象,以便在不创建对象的情况下直接从主类访问它。