两者之间的区别

huangapple go评论95阅读模式
英文:

Difference between the two

问题

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}
package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
}
class non{
    public void greeting(){
        System.out.println("Hej");
    }
}
英文:
package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
}
class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

I know you cannot access a non static method in a static reference but what exactly is the difference between the two and why is one of them working and the other is not?

I tried asking the same question from chatgpt but I got more confused, so I would really appreciate some unadorned answers. Thank you!

答案1

得分: 1

以下是翻译好的内容:

实际上相当简单,
首先让我们确定三件事情

1- static 表示该事物(变量,方法)属于类,因此您可以在不创建实例的情况下访问该(变量,方法)。

2- 使用 this 关键字来访问当前实例

3- main 方法是一个静态方法,它属于类,因此您不能在其中使用 this,因为没有实例。

在您的第一个示例中,虽然您没有明确键入此内容,但类 non 是类 nonstatic 的非静态部分,因此它是一个实例,因此可以使用 this 来调用它,这是可以接受的。

在第二个示例中,类 non 不是类 nonstatic 的一部分,因此没有涉及 this

英文:

It is actually pretty simple,
let's establish 3 things first

1- static means the thing(variable, method) belongs to the class so you can access that (variable, method) without creating instances

2- this keyword is used to access current instance

3- main method is a static method, it belongs to the class, hence you can not use this inside it because there is no instance.

you are not explicitly typing this in your first example but class non is non-static part of class nonstatic so it's an instance so it's called using this which is not acceptable.

the second example, class non is not not part of class nonstatic so there is no this involved

答案2

得分: 0

为了回答您下面的问题,原因是静态方法不依赖于对象。因此,不存在 this

换句话说,您应该认为主方法无法看到您的内部类。如果您想要实例化内部类,需要这样编写它

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new nonstatic().new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

换句话说,静态方法实际上不受对象的限制。
尽管您的 main 方法在类 nonstatic 中,但在访问此静态方法时,不应认为它位于使用 nonstatic 实例化的任何对象中。

英文:

To answer your question below, the reason is that static methods do not depend on objects. Therefore there is no this;

In other words, you should think that the main method cannot see your internal class. If you want to new that inner class, you need to write it like this

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new nonstatic().new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

In other words, static methods are actually free from objects.
Although your main method is in class nonstatic, when accessing this static method, you should not think it is in any object instantiated with nonstatic.

答案3

得分: 0

第一个代码中的 non 类是外部 nonstatic 类的一个 内部类,而第二个代码中的 non 类则保持原样存在。

因此,第一个代码尝试创建一个 non 类的实例,但这需要一个 nonstatic 类的实例,所以它失败了。

正确的方式是按照以下方式创建 nonstatic 类的实例。

请参考 https://stackoverflow.com/questions/12690128/how-to-instantiate-non-static-inner-class-within-a-static-method 以获取更多详细信息。

英文:

The non class of the first code is an inner class of the outer nonstatic class, whereas the non class of the second code exists as it is.

Thus, the first code

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

tries to create an instance of non, but this requires an instance of nonstatic, so it fails.

The proper way is to create an instance of nonstatic as follows.

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new nonstatic().new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

Refer to https://stackoverflow.com/questions/12690128/how-to-instantiate-non-static-inner-class-within-a-static-method for more details.

答案4

得分: 0

根据非静态嵌套类

就像实例变量和方法一样,内部类与封装类的实例相关联

要实例化内部类,我们必须首先实例化其封装类。

如果要创建内部类的实例,应该这样做:

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

所以要么:

  1. 将类设置为非静态类,或者
  2. 创建非A的实例,例如 non A = new nonstatic().new non();
static class non {
    public void greeting() {
        System.out.println("Hej");
    }
}
英文:

According to Non-Static Nested Classes,

> Just like instance variables and methods, inner classes are associated with an instance of the enclosing class

> To instantiate an inner class, we must first instantiate its enclosing class.

If you want to create an instance of the inner class, you should do this:

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}
OuterClass outerObject = new OuterClass()
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

So either:

  1. make class non as static class, or
  2. create instance non A like non A = new nonstatic().new non();
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

答案5

得分: 0

以下是翻译好的内容:

第一个示例不起作用的原因是因为“new non()”没有上下文。

由于“non”是一个内部类,您需要从“nonstatic”的一个实例中访问它。

例如,

nonstatic nonstatic = new nonstatic();
non A = nonstatic.new non();

虽然“non”是嵌套的,但您可以将其声明为静态;这样更容易引用。

public static void main(String[] args) {
    non A = new nonstatic.non();
}
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

此外,“nonstatic”标识符是多余的;因此,您可以将分配保持不变。

public static void main(String[] args) {
    non A = new non();
}
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

在第二个示例中,您只是有两个非嵌套的类。
这在Java中没有太多用途;尽管我想在某些情况下可能会有所帮助。

基本上,您可以在一个文件中有多个类,只要不声明多个为“public”。

所以,例如,以下将引发错误。

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
}
public class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

错误

java: class non is public, should be declared in a file named non.java

这是关于类和嵌套类的Java教程。
Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

并且,这是一个与您的第二个示例相关的讨论。
StackOverflow – Can a java file have more than one class?

英文:

> "... what exactly is the difference between the two and why is one of them working and the other is not? ..."

The reason the first example is not working is because new non() has no context.

Since non is an inner-class you'll need to access it from an instance of nonstatic.

For example,

nonstatic nonstatic = new nonstatic();
non A = nonstatic.new non();

Although, since non is nested, you could declare it as static; making it easier to reference.

public static void main(String[] args) {
    non A = new nonstatic.non();
}
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

Additionally, the nonstatic identifier is redundant; so you could just leave the assignment the way it is.

public static void main(String[] args) {
    non A = new non();
}
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

In your second example you simply have two classes, non-nested.
This doesn't have much use in Java; although I imagine it may help in certain situations.

Essentially, you can have more than one class within a file, as long as not more than one is declared as public.

So, for example, the following will cause an error.

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
}
public class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

Error

java: class non is public, should be declared in a file named non.java

Here is the Java tutorial on classes and nested classes.
Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects).

And, here is a relevant discussion regarding your second example.
StackOverflow – Can a java file have more than one class?.

huangapple
  • 本文由 发表于 2023年7月18日 09:15:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定