Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do static Create methods exist?

I was wondering, why do static Create methods exist?

For instance, why use this code:

System.Xml.XmlReader reader = System.Xml.XmlReader.Create(inputUri);

over this code:

System.Xml.XmlReader reader = new System.Xml.XmlReader(inputUri);

I cannot find the rationale for using one over the other, and can't find any relation between classes who use this construct over the other.

Can anyone shed some light on this?

like image 996
GeReV Avatar asked Mar 18 '10 13:03

GeReV


People also ask

Why do static methods exist?

Static Methods Are Often Utility Methods They can be employed by different classes without having to create an instance, which can sometimes make the difference. Methods are created as static when object state has no effect on their behavior because they depend only on their own parameters.

What is the purpose of static method in Java?

A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method.

What is the advantage of making a method static?

A static method belongs to the class rather than the object of a class. A static method can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.


1 Answers

XmlReader is an abstract class. You cannot instantiate it.

Providing a Create method is an instance of the factory pattern. Depending on the specified arguments a different implementation of XmlReader is chosen and returned. For example, there are validating and non-validating XmlReader implementations in the .NET framework.

like image 109
dtb Avatar answered Oct 23 '22 14:10

dtb