“静态"方法的词源是什么? [英] What's the etymology of 'static' method?

查看:138
本文介绍了“静态"方法的词源是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多语言中,例如在Java和JavaScript中,"static"关键字是指类方法或类变量.

  • 为什么将其称为静态"方法?为什么不使用蓝色"方法?还是全局"方法?
  • 关键字来自哪里?该教派的起源是什么?
  • 与编译器有关吗?

解决方案

回答您的第一个和第二个问题
静态方法属于类本身,因此静态方法也称为类方法.
例如, main() 方法必须是静态的,以便Java虚拟机可以在不创建类实例的情况下调用它来运行Java程序.
由于静态方法不依赖于类对象,即实例在Java中谈论继承,因此该方法始终由引用调用,而不管其指向的实例对象是什么.

回答第三个问题
当编译器编译该类时,它会在编译时决定为每个静态方法调用调用哪个确切方法(与非静态方法调用的最大区别是:在这种情况下,仅在运行时才确定要调用的确切方法). 只是要添加到Java中的静态方法
如果编译后的静态最终值是编译时初始化的原语,则直接保存在类本身中.

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?

解决方案

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.

这篇关于“静态"方法的词源是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆