我可以使用类的方法而不实例化这个类吗?

我可以使用类的方法而不实例化这个类吗?

问题描述:

我有一个有几个方法的类,这些方法中没有构造函数。

I have a class with several methods and there is no constructor among these methods.

所以,我想知道是否可以调用类的方法没有创建类的实例。

So, I am wondering if it is possible to call a method of a class without a creation of an instance of the class.

例如,我可以做类似的事情:

For example, I can do something like that:

NameOfClass.doMethod(x1,x2,...,xn)



In general I do not see why it should be impossible. I just call a function which does something (or return some values). If it is possible, what will happen if the method sets a value for a private variable of the class. How can I reach this value? In the same way?

NameOfClass.nameOfVariable


它被称为静态变量和静态方法。试试吧,看看它是否可以编译。

It's called static variables and static methods. Just try it and see that it compiles.