I Am getting a warning message in my class, like
Add a
Protected
constructor or thestatic
keyword to the class declaration
The error is gone after I tried both the below ways,
static
class without constructor
public static class Program {
}
static
class with protected
using constructorpublic class Program
{
protected Program() { }
}
So What is the difference between Static Class vs Protected Constructor which is mentioned in my above solution? And which one is best to use?
Constructors are not allowed to be static in Java because of the following reason: In Java, static methods and variables apply to the classes. But a constructor is called when a new operator is used to create an instance. Since it does not belong to the property class, it is not allowed to be static.
Static classes cannot contain an instance constructor. However, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.
A Sealed class cannot be inherited from and can static and non-static members or methods. Contains a public constructor. 2. A Static class cannot be inherited from and contains only static methods and class properties.
yes we can have static constructor inside a non-static class. Yes, it can. But user will not have any control on its invoke.
A static
class doesn't need an instance to access its members. A static
class cannot have instance members (e.g. public int MyNumber;
is not allowed on a static class because only static members are allowed on a static class). Both instance and static members are allowed on a non-static class though. A class with a protected
constructor can only have an instance created by itself or something that inherits from it.
public class Program
{
protected Program()
{
// Do something.
}
public static Program Create()
{
// 100% Allowed.
return new Program();
}
public void DoSomething()
{
}
}
public static class AnotherClass
{
public static Program CreateProgram()
{
// Not allowed since Program's constructor is protected.
return new Program();
}
}
public class SubProgram : Program
{
protected SubProgram()
{
// Calls Program() then SubProgram().
}
public new static Program Create()
{
// return new Program(); // We would need to move the SubProgram class INSIDE the Program class in order for this line to work.
return new SubProgram();
}
}
Program.Create(); // Can be called since Create is public and static function.
Program.DoSomething() // Can't be called because an instance has not been instantiated.
var test = Program.Create();
test.DoSomething(); // Can be called since there is now an instance of Program (i.e. 'test').
AnotherClass.CreateProgram(); // Can't be called since Program's constructor is protected.
SubProgram.Create(); // Can be called since SubProgram inherits from Program.
As for performance, this distinction doesn't really have much to do with performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With