Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Class vs Protected Constructor

I Am getting a warning message in my class, like

enter image description here

Add a Protected constructor or the static keyword to the class declaration

Solution

The error is gone after I tried both the below ways,

static class without constructor

public static class Program    {
    }

Non static class with protected using constructor

public  class Program
    {
        protected Program() { }
    }

Question:

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?

like image 894
Ramesh Rajendran Avatar asked Jan 05 '18 13:01

Ramesh Rajendran


People also ask

Can we use static or protected for constructor?

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.

Can static class have constructor?

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.

What is difference between sealed and static class?

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.

Can static class have non-static constructor?

yes we can have static constructor inside a non-static class. Yes, it can. But user will not have any control on its invoke.


1 Answers

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.

like image 160
Shelby115 Avatar answered Sep 20 '22 12:09

Shelby115