什么是静态方法和变量? [英] What are static method and variables?

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

问题描述

有人能给出一个容易理解的静态变量和静态方法的定义吗?

Can someone give an easily comprehensible definition of a static variable and a static method?

这些与非静态变量和方法相比如何?

How do these compare to non-static variables and methods?

推荐答案

在Java中, static 表示类方法和类变量(与实例方法和实例变量相对)。可以在不存在实例的情况下访问这些方法和变量。

In Java, static denotes class methods and class variables (as opposed to instance methods and instance variables). These methods and variables can be accessed without an instance present.

将此与实例方法和实例变量进行对比:必须通过对象访问它们。例如, length()对一个对象进行操作:

Contrast this to instance methods and instance variables: they must be accessed through an object. For example, length() operates on an object:

String a = "hello";
int len = a.length();

相比之下, valueOf 无法运作宾语;此外,它在调用时创建一个新对象:

In contrast, valueOf cannot operate on an object; moreover, it creates a new object when called:

String x = String.valueOf(123.45);

注意如何使用< objectName> 后跟一个点,而使用< className> 访问静态方法后跟一个点

Note how instance methods are called using <objectName> followed by a dot ., while static methods are accessed using <className> followed by a dot ..

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

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