主类java中是静态的,构造函数中是非静态的

发布于 2024-12-10 20:27:57 字数 203 浏览 0 评论 0原文

我只是想看看我是否能够完全理解静态的概念以及主类中静态的原因。关键字 static 指的是主类。主类中的方法之所以是静态的,是因为主类不处理对象,而是处理类本身。

然而,构造函数处理对象,因此使用非静态构造函数,因为对象具有独特的特征,将它们设为静态是没有意义的。

如果有人能看出我的陈述中是否犯了错误,或者可以引导我走向正确的方向,那将对我有很大帮助! :)

I'm just trying to see if I can fully understand the concept of static and the reason of static in a main class. The keyword static refers to the main class. the reason why methods in a main class are static is because the main class does not deal with objects but with the class itself.

However constructors deal with objects and therefore uses non static constructors because objects have unique characteristics and it would not make sense to make them static.

If anyone can see if I made a mistake in my statement or can steer me in the right direction, it would help me a great deal! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

平安喜乐 2024-12-17 20:27:57

我只是想看看我是否能完全理解静态的概念以及主类中静态的原因。

Java 中没有主类这样的东西。

关键字static指的是主类。

不,它指的是静态类或静态类成员。

主类中的方法之所以是静态的,是因为主类不处理对象,而是处理类本身。

没有主类这样的东西。该声明毫无意义。

然而,构造函数处理对象,因此使用非静态构造函数,因为对象具有独特的特征,将它们设为静态是没有意义的。

所有构造函数都是“非静态”的。不存在静态构造函数这样的东西。这种讨论毫无意义。

如果有人能看出我的陈述中是否犯了错误,或者可以引导我走向正确的方向,这将对我有很大帮助! :)

我认为你需要重新开始,忘记不存在的“主类”和“静态构造函数”。基本上静态方法是指无需类实例即可调用的方法。相反,构造函数创建类的实例,因此它们在逻辑上不能是静态的。

I'm just trying to see if I can fully understand the concept of static and the reason of static in a main class.

There is no such thing as a main class in Java.

The keyword static refers to the main class.

No, it refers to static classes or static class members.

The reason why methods in a main class are static is because the main class does not deal with objects but with the class itself.

There is no such thing as a main class. The statement is meaningless.

However constructors deal with objects and therefore uses non static constructors because objects have unique characteristics and it would not make sense to make them static.

All constructors are 'non-static'. There is no such thing as a static constructor. There is no point in any of this discussion.

If anyone can see if I made a mistake in my statement or can steer me in the right direction, it would help me a great deal! :)

I think you need to start again, forgetting about non-existent 'main classes' and 'static constructors'. Basically static methods refer to methods that can be invoked without having an instance of the class. Conversely, constructors create an instance of the class so they can't logically be static.

各空 2024-12-17 20:27:57

来自 Java 规范(第三版):

关于静态字段 http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.1.1

如果一个字段被声明为静态,则恰好存在一个化身
字段,无论该类有多少个实例(可能为零)
最终可能会被创建。静态字段,有时称为类
变量,在类初始化时化身

关于静态方法 http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.3.2

声明为静态的方法称为类方法。 A类
方法始终在不引用特定对象的情况下调用。一个
尝试使用关键字 this 或 the 引用当前对象
关键字 super 或引用任何周围的类型参数
类方法体中的声明会导致编译时
错误。声明静态方法时会出现编译时错误
摘要。

From Java Specification (Third edition):

About static fields http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.1.1

If a field is declared static, there exists exactly one incarnation of
the field, no matter how many instances (possibly zero) of the class
may eventually be created. A static field, sometimes called a class
variable, is incarnated when the class is initialized

About static methods http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.3.2

A method that is declared static is called a class method. A class
method is always invoked without reference to a particular object. An
attempt to reference the current object using the keyword this or the
keyword super or to reference the type parameters of any surrounding
declaration in the body of a class method results in a compile-time
error. It is a compile-time error for a static method to be declared
abstract.

强者自强 2024-12-17 20:27:57

不完全是...

static 意味着字段/方法等属于,而不是该类的特定实例。该类的所有实例都可以访问静态字段,并且每个静态字段只有一个实例在实例之间共享。

main 方法必须是静态的,因为它是在不先创建实例的情况下调用的。

所有静态方法都可以在不创建类实例的情况下调用 - 主方法调用的任何方法也必须是静态的,除非主方法创建类的实例 - 然后可以在该实例上调用实例方法。

Not exactly...

static means the field/method etc belongs to the class, and not to a particular instance of the class. All instances of the class have access to static fields, and there is only one instance of each static field that is shared between instances.

The main method must be static, because it is invoked without creating an instance first.

All static methods may be invoked without creating an instance of the class - any methods the main method calls must also be static, unless the main method creates an instance of the class - then instance methods may be invoked on that instance.

冷︶言冷语的世界 2024-12-17 20:27:57
public class MyClass { // This name will dictate the name of your file

    public Sting myVariable = ""; // Field or Global variable

    public void MyClass() { // Constructor
    }

    public static void main (String[] args){ // This is the "main" "Method"
        MyClass.print("Hello "); // Static method call

        MyClass myClass = new MyClass(); // Non-static method call
        myClass.print("World");
    }

    public static void print(String s){ // Static method
        System.out.print(s);
    }

    public void print(String s){ // Non-static method
        this.myVariable = s; // Changing the value of a field/global variable
        System.out.print(s);
    }
}

静态方法调用 - 当您进行静态方法调用时,您不会对类/对象内部的数据进行持久更改。

非静态方法调用 - 对于这种类型的方法调用,您必须使用构造函数方法(不能是静态的)“实例化”对象。然后可以传递一个对象。如果您的方法更改了类中字段/全局变量的值,则该值在该对象中保持不变,直到有人/其他东西更改它为止。

当您实例化一个对象时,您只能“调用”该对象内的非静态方法。同样,您不能从对象调用静态方法,您必须通过提供类名、后跟句点和方法名来调用它。

静态方法只能引用其他静态内容。非静态可以引用两者。

public class MyClass { // This name will dictate the name of your file

    public Sting myVariable = ""; // Field or Global variable

    public void MyClass() { // Constructor
    }

    public static void main (String[] args){ // This is the "main" "Method"
        MyClass.print("Hello "); // Static method call

        MyClass myClass = new MyClass(); // Non-static method call
        myClass.print("World");
    }

    public static void print(String s){ // Static method
        System.out.print(s);
    }

    public void print(String s){ // Non-static method
        this.myVariable = s; // Changing the value of a field/global variable
        System.out.print(s);
    }
}

STATIC METHOD CALL - When you make a static method call you do not make a lasting change to the data inside of the class/object.

NON-STATIC METHOD CALL - For this type of method call you must "instantiate" the object using the Constructor method (which cannot be static). An object can then be passed around. If your method changes the value of a field/global variable in the class then that value remains the same in that object until someone/something else changes it.

When you instantiate an object you can only "invoke" (call) non-static methods inside that object. Likewise you cannot call static methods from an object you must call it by providing the class name followed by a period followed by the method name.

Static methods can only reference other static content. Non-static can reference both.

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