问题
In many languages, e.g. Java and JavaScript, the 'static' keyword refers to class methods or class variables.
- Why is it called the 'static' method? Why not 'blue' method? Or 'global' method?
- Where does the keyword come from? What is the origin for that denomination?
- Is it related to the compiler?
回答1:
To answer to ur 1st and 2nd question
A static method belongs to the class itself so a static method is also called class method.
For example The main()
method must be static so the Java Virtual Machine can invoke its without create an instance of the class, to run a Java program.
Since static method does not depend on class object i.e. instance talking about inheritance in Java the method is always called by the reference irrespective of the instance Object it is pointing at.
To answer to ur third question
When the compiler compiles that class it decides at compile time which exact method is called for each static method call (that's the big difference to non-static method calls: the exact method to be called is only decided at run time in those cases).
just to add to static method in Java
The static final values in compiled version saved directly in the class itself, if they are compile-time initialized primitives.
来源:https://stackoverflow.com/questions/60788631/whats-the-etymology-of-static-method