What's the etymology of 'static' method? [duplicate]

醉酒当歌 提交于 2020-04-30 08:43:56

问题


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

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