我想知道什么时候使用静态方法?假设我有一个类,有几个getter和setter,一个或两个方法,我希望这些方法只能在类的实例对象上调用。这是否意味着我应该使用静态方法?

例子:

Obj x = new Obj();
x.someMethod();

或:

Obj.someMethod(); // Is this the static way?

我很困惑!


当前回答

使用静态方法有一些合理的理由:

Performance: if you want some code to be run, and don't want to instantiate an extra object to do so, shove it into a static method. The JVM also can optimize static methods a lot (I think I've once read James Gosling declaring that you don't need custom instructions in the JVM, since static methods will be just as fast, but couldn't find the source - thus it could be completely false). Yes, it is micro-optimization, and probably unneeded. And we programmers never do unneeded things just because they are cool, right? Practicality: instead of calling new Util().method(arg), call Util.method(arg), or method(arg) with static imports. Easier, shorter. Adding methods: you really wanted the class String to have a removeSpecialChars() instance method, but it's not there (and it shouldn't, since your project's special characters may be different from the other project's), and you can't add it (since Java is somewhat sane), so you create an utility class, and call removeSpecialChars(s) instead of s.removeSpecialChars(). Sweet. Purity: taking some precautions, your static method will be a pure function, that is, the only thing it depends on is its parameters. Data in, data out. This is easier to read and debug, since you don't have inheritance quirks to worry about. You can do it with instance methods too, but the compiler will help you a little more with static methods (by not allowing references to instance attributes, overriding methods, etc.).

如果你想创建一个单例,你还必须创建一个静态方法,但是…不喜欢。我的意思是,三思而后行。

更重要的是,为什么不想创建静态方法?基本上,多态被抛在了脑后。你不能重写这个方法,也不能在接口中声明它(在java 8之前)。这给你的设计带来了很大的灵活性。此外,如果您需要状态,如果您不小心,您将以大量并发错误和/或瓶颈告终。

其他回答

如果将静态关键字应用于任何方法,则称为静态方法。

静态方法属于类,而不是类的对象。 不需要创建类实例而调用的静态方法。 方法可以访问静态数据成员,并可以更改它的值。 静态方法可以通过类名。静态名来访问。示例:Student9.change(); 如果要使用类的非静态字段,则必须使用非静态方法。

//改变所有对象的公共属性的程序(静态字段)。

class Student9{  
 int rollno;  
 String name;  
 static String college = "ITS";  

 static void change(){  
 college = "BBDIT";  
 }  

 Student9(int r, String n){  
 rollno = r;  
 name = n;  
 }  

 void display (){System.out.println(rollno+" "+name+" "+college);}  

public static void main(String args[]){  
Student9.change();  

Student9 s1 = new Student9 (111,"Indian");  
Student9 s2 = new Student9 (222,"American");  
Student9 s3 = new Student9 (333,"China");  

s1.display();  
s2.display();  
s3.display();  
}  }

O/P: 111印度BBDIT 222美国BBDIT 333中国BBDIT

实际上,我们在类中使用静态属性和方法,当我们想要使用程序的某些部分时,它们应该一直存在,直到程序运行为止。我们知道,要操作静态属性,我们需要静态方法,因为它们不是实例变量的一部分。如果没有静态方法,操作静态属性是非常耗时的。

我想知道什么时候使用静态方法?

静态方法的一个常见用途是访问静态字段。 但是你可以有静态方法,而不需要引用静态变量。不引用静态变量的Helper方法可以在一些java类中找到,如java.lang. math 公共静态int min(int a, int b) { 返回(a <= b) ?A: b; } 另一个用例,我认为这些方法与同步方法相结合是在多线程环境中实现类级锁定。

假设我有一个类,有几个getter和setter,一个或两个方法,我希望这些方法只能在类的实例对象上调用。这是否意味着我应该使用静态方法?

如果你需要访问类的实例对象上的方法,你的方法应该是非静态的。

Oracle文档页提供了更多详细信息。

不是所有的实例和类变量和方法的组合都被允许:

实例方法可以直接访问实例变量和实例方法。 实例方法可以直接访问类变量和类方法。 类方法可以直接访问类变量和类方法。 类方法不能直接访问实例变量或实例方法——它们必须使用对象引用。而且,类方法不能使用this关键字,因为没有this的实例可以引用。

静态方法不需要在对象上调用,这就是你使用它的时候。例如:你的Main()是一个静态的,你没有创建一个对象来调用它。

使用静态方法有一些合理的理由:

Performance: if you want some code to be run, and don't want to instantiate an extra object to do so, shove it into a static method. The JVM also can optimize static methods a lot (I think I've once read James Gosling declaring that you don't need custom instructions in the JVM, since static methods will be just as fast, but couldn't find the source - thus it could be completely false). Yes, it is micro-optimization, and probably unneeded. And we programmers never do unneeded things just because they are cool, right? Practicality: instead of calling new Util().method(arg), call Util.method(arg), or method(arg) with static imports. Easier, shorter. Adding methods: you really wanted the class String to have a removeSpecialChars() instance method, but it's not there (and it shouldn't, since your project's special characters may be different from the other project's), and you can't add it (since Java is somewhat sane), so you create an utility class, and call removeSpecialChars(s) instead of s.removeSpecialChars(). Sweet. Purity: taking some precautions, your static method will be a pure function, that is, the only thing it depends on is its parameters. Data in, data out. This is easier to read and debug, since you don't have inheritance quirks to worry about. You can do it with instance methods too, but the compiler will help you a little more with static methods (by not allowing references to instance attributes, overriding methods, etc.).

如果你想创建一个单例,你还必须创建一个静态方法,但是…不喜欢。我的意思是,三思而后行。

更重要的是,为什么不想创建静态方法?基本上,多态被抛在了脑后。你不能重写这个方法,也不能在接口中声明它(在java 8之前)。这给你的设计带来了很大的灵活性。此外,如果您需要状态,如果您不小心,您将以大量并发错误和/或瓶颈告终。