static in the main class java and non static in constructor [closed]

我只是一个虾纸丫 提交于 2019-12-20 07:32:13

问题


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! :)


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/7801304/static-in-the-main-class-java-and-non-static-in-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!