When should I use static methods in a class and wh

2019-01-05 10:41发布

I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need to call a method of a class, I create an object of that class and call the desired method.

Q: Static variable in a method holds it's value even when method is executed but accessible only in its containing method but what is the best definition of static method?

Q: Is calling the static method without creating object of that class is the only benefit of static method?

Q: What is the accessible range for static method?

Thanks

10条回答
smile是对你的礼貌
2楼-- · 2019-01-05 10:50

The only time you want to use a static method in a class is when a given method does not require an instance of a class to be created. This could be when trying to return a shared data source (eg a Singleton) or performing an operation that doesn't modify the internal state of the object (String.format for example).

This wikipedia entry explains static methods pretty well: http://en.wikipedia.org/wiki/Method_(computer_science)#Static_methods

查看更多
放我归山
3楼-- · 2019-01-05 10:51

Static variables and static methods are bound to the class, and not an instance of the class.

Static methods should not contain a "state". Anything related to a state, should be bound to an instantiated object, and not the class.

查看更多
何必那么认真
4楼-- · 2019-01-05 10:51

Static Methods in PHP:

Can be called without creating a class object.

Can only call on static methods and function.

查看更多
混吃等死
5楼-- · 2019-01-05 10:52

Essentially, static methods let you write procedural code in an object oriented language. It lets you call methods without having to create an object first.

查看更多
贪生不怕死
6楼-- · 2019-01-05 10:54

Static variable is used when you want to share some info between different objects of the class.As variable is shared each object can update it and the updated value be available for all other objects as well. As static variable can be shared,these are often called as class variable.

查看更多
Lonely孤独者°
7楼-- · 2019-01-05 10:57

Your description of a static variable is more fitting to that found in C. The concept of a static variable in Object Oriented terms is conceptually different. I'm drawing from Java experience here. Static methods and fields are useful when they conceptually don't belong to an instance of something.

Consider a Math class that contains some common values like Pi or e, and some useful functions like sin and cos. It really does not make sense to create separate instances to use this kind of functionality, thus they are better as statics:

// This makes little sense
Math m = new Math();
float answer = m.sin(45);

// This would make more sense
float answer = Math.sin(45);

In OO languages (again, from a Java perspective) functions, or better known as methods, cannot have static local variables. Only classes can have static members, which as I've said, resemble little compared to the idea of static in C.

查看更多
登录 后发表回答