Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between static, internal and public constructors?

What is the difference between static, internal and public constructors? Why do we need to create all of them together?

 static xyz()  {  }   public xyz()  {  }   internal xyz()  {  } 
like image 586
Shankaranarayana Avatar asked Aug 10 '11 07:08

Shankaranarayana


People also ask

What is the difference between public and static constructor in Java?

Static constructor called only the first instance of the class created but the public constructor called every time that instance of the class is created. A constructor declared using a static modifier is a static constructor.

What is the difference between public static and public static methods?

public simply means that users of the class can call that constructor (as opposed to, say, private). static means that the method (in this case the constructor) belongs not to an instance of a class but to the “class itself”. In particular, a static constructor is called once, automatically, when the class is used for the first time.

What is the difference between a public and an internal constructor?

The two are essentially the same. One argument I've seen for distinguishing between them is that making your constructor internal ensures the type will only ever be instantiated by types within the current assembly, even if it is later decided that the type itself should be public instead of internal.

What is the use of a static constructor?

A static constructor is use to initialize static data or to perform a particular action that need to be performed only once in life cycle of class. Static constructor is first block of code to execute in class. Static constructor executes one and only one time in life cycle of class. It is called automatically.


2 Answers

The static constructor will be called the first time an object of the type is instantiated or a static method is called. And will only run once

The public constructor is accessible to all other types

The internal constructor is only accessible to types in the same assembly

On top of these three there's also protected which is only accessible to types derived from the enclosing type

and protected internal which is only accessible to types in the same assembly or those that derives from the enclosing type

and private which is only accessible from the type itself and any nested types

like image 82
Rune FS Avatar answered Oct 05 '22 19:10

Rune FS


The difference between public and internal is that the internal constructor can only be called from within the same assembly, while the public one can be called from other assemblies as well.

static is a constructor that gets called only the first time the class is referenced. Static members do not belong to an instance of the class, but "to the class itself". See http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx for more information about static.

like image 21
C.Evenhuis Avatar answered Oct 05 '22 18:10

C.Evenhuis