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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:59:34  来源:igfitidea点击:

What is the purpose of creating static object in Java?

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 staticmethod,

在您的情况下,您尝试在static方法中访问资源,

public static void main(String[] args)

Thus anything we access here without creating an instance of the class Flavor1Demohas to be a staticresource.

因此,我们在不创建类的实例的情况下访问的任何内容都Flavor1Demo必须是static资源。

If you want to remove the statickeyword from Democlass, your code should look like:

如果您想staticDemo类中删除关键字,您的代码应如下所示:

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 Flavor1Demoand then get the non-staticresource dThe above code wont complain of compilation errors.

在这里你看到,我们已经创建了一个实例,Flavor1Demo然后获取non-static资源d上面的代码不会抱怨编译错误。

Hope it helps!

希望能帮助到你!

回答by Yousaf

You get an error by removing statickeyword from static Demo d = new Demo()because you are using that object dof class Demoin mainmethod which is static. When you remove statickeyword from static Demo d = new Demo(), you are making object dof your Democlass non-staticand non-staticobject cannot be referenced from a staticcontext.

删除static关键字 from会导致错误,static Demo d = new Demo()因为您在方法中使用d了该类的对象。当您删除的关键字,你正在对象你的类和对象不能从引用的上下文。Demomainstaticstaticstatic Demo d = new Demo()dDemonon-staticnon-staticstatic

If you remove d.show();from mainmethod and also remove statickeyword 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 showmethod of Democlass, you would have to create an object of your Democlass inside mainmethod.

现在,如果要调用类的show方法,则Demo必须Demomain方法内部创建类的对象。

public static void main(String[] args){
     Demo d = new Demo(); 
     d.show();
 }

回答by Tomasz Mularczyk

Thats because you try to use dthat 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 thishere. If you set das non-static, it will only exist in an instance of Flavor1Demoso it can't be access from the mainunless you build a instance first (new Flavor1Demo().d.show();

为了简单起见,这意味着它不存在于 的实例中Flavor1Demothis此处不存在。如果您设置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.

为了在不创建对象的情况下访问主类中的方法或变量,这里我们定义了匿名内部类,在该类中我们创建了静态类型的对象,以便在不创建对象的情况下直接从主类访问它。