Static Method With Class Name Also?
I'm looking at a piece of co开发者_StackOverflowde I don't fully understand. This is how it looks:
public static ClassName MethodName(int parameter){
//Method does its thing
}
Is there a name for this type of code pattern? Also, what is it being used for?
It's simply a static method which returns an object of type ClassName
. Do you mean that it is declared inside of the ClassName
class? This is fine, it just means that the class in question has a static method which returns an instance of itself. This is commonly used in the Factory Pattern, for example. Rather than manually instantiate an instance of the class, a static method is called which returns an instance.
It is simply a static method that returns an object of type "ClassName". Looks like it could be a factory method(?)
Also, what is it being used for?
Can't really answer that seeing as there is no definition in your example. It's just a method signature with generic names.
In you example the method returns an instance of class ClassName
.
This is really equivalent to any other method, equivalent to public static String MethodName()
. It's fairly common to create a static method on a class, which returns an instance of the same class, i.e. a factory method.
It's just a class method. You don't have to instantiate anything to call it. It can only access other static methods and fields, because without an instance you can't call any non-static(non-class) variables.
精彩评论