java中static关键字的用途是什么 [英] what is the purpose of the static keyword in java

查看:79
本文介绍了java中static关键字的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在java中使用静态和java中静态的目的是什么

解决方案

互联网上有太多的信息 -

静态关键字及其在Java中的用法 [< a href =http://www.codeproject.com/Articles/703533/Static-Keyword-and-Its-Usage-in-Javatarget =_ blanktitle =New Window> ^ ]

Java静态关键字 [ ^ ]

Java Static方法,变量,静态块和带示例的类 [ ^ ]

关键字'静态'是什么意思? [ ^ ]

静态(关键字) [ ^ ]


目的,如果字段,属性和方法非常不同。对于字段和属性,静态对象是类对象;这个班级只有一个这样的成员;如果类没有实例化,那么这个成员就有一个例子;如果你实例化了几个类的实例,这仍然是这个成员的一个实例。



静态方法只是没有引用this的方法当调用实例方法时,它作为隐式参数传递。因此,自然地,静态方法不能访问任何实例成员,而只能访问该类的其他静态成员。



现在,使用它...实际上,静态方法非常重要。如果只有参数中的所有输入,并且输出为void或者所有都放在返回值中,则应始终使用静态方法。不需要访问实例,这将是多余的,甚至更容易出错。对于那些熟悉非OOP语言的人:这非常类似于旧式的非OOP功能。无法访问对象,没有对象。



您还可以访问任何其他静态成员,首先是其他静态方法。您还可以访问静态字段和属性,但它是否很好?问题是:应谨慎使用静态字段和属性本身。人们可以完全避免它们;过度使用它们可以被视为编程风格不佳的标志。如果需要,您始终可以使用实例成员并创建实例,只创建一个实例。也就是说,它与单例模式有关,它本身应该小心使用。因此,单例可以使用静态成员实现。请参阅: http://en.wikipedia.org/wiki/Singleton_pattern



对于Java实现,请参阅:

http: //www.javaexperience.com/design-patterns-singleton-design-pattern

http://www.javaworld.com/article/2073352/core-java/simply-singleton.html



从语法上讲,所有静态成员都是通过类的名称访问的,而不是通过引用该类实例的任何变量/成员的名称来访问。



< DD> -SA

wy we use static in java and what is the purpose of static in java

解决方案

There is so much information available on the internet -
Static Keyword and Its Usage in Java[^]
Java static keyword[^]
Java Static Methods, Variables, Static Block and Class with Example[^]
What does the keyword 'static' mean?[^]
Static (keyword)[^]


The purpose if very different for fields, properties and methods. For fields and properties, static objects are the class objects; there is only one instance of such member for the class; if the class is not instantiated at all, there is one instance of such member; if you instantiate several instanced of the class, this is still one instance of this member.

Static methods are just the methods which don't have that "this" referenced which is passed as an implicit argument when one calls an instance method. So, naturally, static methods don't have access to any instance members, but only to other static members for the class.

Now, the use of it… Actually, static methods are very important. If you have all input only in arguments, and output is void or all is put in return value, you should always use a static method. The access to the instance is not needed, would be superfluous and even more error-prone. For those familiar with non-OOP languages: this is pretty much like old-day non-OOP function. No access to objects, no objects.

You can also access any other static members, first of all, other static methods. You can also access static fields and properties, but is it good? The thing is: static fields and properties themselves should be used with care. One can avoid them totally; and excessive use of them can be considered as a sign of bad programming style. You can always use instance members and create an instance, if required, only one instance. That said, it has something to do with the singleton pattern, which itself should be used with care, minimally. So, the singleton can be implemented using static members. Please see: http://en.wikipedia.org/wiki/Singleton_pattern.

For Java implementations, please see:
http://www.javaexperience.com/design-patterns-singleton-design-pattern,
http://www.javaworld.com/article/2073352/core-java/simply-singleton.html.

Syntactically, all static members are accessed via the name of the class, not via the name of any variable/member referencing instance of the class.

—SA


这篇关于java中static关键字的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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