Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Sorry guys for stupid question.I undarstand that constructor calls only once in the program,but i don't understand in anyway when i should use a constructor.
Posted

Are you sure this is a valid answerable question? This is a wrong approach to understanding things.

You don't need to look for benefits. Instead, you need to 1) understand what exactly one or another language and platform feature does; 2) analyze what do you need to achieve; 3) based on that previous item, choose the expressive feature of language which suits you more.

Don't even try to think in terms of "benefits". Your final decision should provide benefits. Language features do not have benefits, they all are needed (in a good language, of course). It all depends on purpose.

Comparison on the two kinds of method simply makes no sense. But you can compare two or more use cases. Let's consider them.
C#
// case 1:
class MyClass {
    internal static MyClass Create(string initializationParameter) {
        return new MyClass(string)
    }
    private MyClass(string initializationParameter) { /* ... */ }
}

// vs simple
class MyClass {
    internal MyClass(string initializationParameter) { /* ... */ }
}
Here, you can instantiate MyClass in different ways, and it's quite apparent that first way is excessive and pointless. But there are cases where first approach (which we can call factory method makes a lot of sense).

Consider one use case from the .NET FCL, the class System.Net.WebRequest:
http://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx[^].

Let's understand why you create instances of this class not via its constructors, but via its factory methods Create? These two:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.create%28v=vs.110%29.aspx[^].

This class is abstract. So, not having its instance, you can call only the static members, including the factory methods. These methods will return you the instance of this class, but runtime types of this instance cannot be of the abstract class. These runtime types of the instances will be some derived, non-abstract type. The actual type to instantiate depends on the initialization parameter representing URL. Depending on the URL scheme ("http://", "htts://", "ftp://"), the factory methods chose appropriate derived implementation class. This mechanism leverage "true" OOP, late binding based on virtual methods.

—SA
 
Share this answer
 
v2
Comments
DamithSL 14-Nov-14 16:59pm    
5wd!
Sergey Alexandrovich Kryukov 14-Nov-14 17:01pm    
Wow, you are really fast! Thank you, Damith.
—SA
Inimicos wrote:
constructor calls only once in the program


That's true of static constructors, not of instance constructors -- which are you asking about?
 
Share this answer
 
v2
Comments
Inimicos 14-Nov-14 15:35pm    
I mean for example i create user constructor in some class,in this costructor i entered some varibles.Then i create instance of this class.I can create object by default constructor and add this varbles or user constructor which contain this varibles in it.What is advanteges of using user constructos.
Constructors initialize objects: they put the objects (instances of a classes) in a know state (more at Wikipedia[^]).

On the other hand, static methods serve to a very different purpose: they are 'class methods' and don't deal with class instances.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Nov-14 17:00pm    
You are perfectly right, but you putting yourself in an awkward situation: discussing "differences" (it's good you wasn't provoked into discussing "benefits"), while the question should not be answered at all in the way expected by OP; this is a just wrong way of thinking.

At the same time, the question has a rational grain: different patterns of initialization, from the user's point of view.

I tries to explain the consideration involved. Please see my Solution 3.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900