Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private static fields in non static class [closed]

I have the following class:

class Application
{
    private Process _process; 

    private static string _applicationPath = string.Empty;

    public void Start(string arguments)
    {
        if (!File.Exists(_applicationPath))
            Deploy();
        _process = Process.Start(_applicationPath, arguments);
    }

    public void SomeMethod()
    {
        //method that manipulate _process
    }

    private void Deploy()
    {
        // copying, installation steps (takes some time) and assign _applicationPath
    }
}
  1. Is it bad to use a static field here? I expect that other instances of Application will use the same _applicationPath.
  2. Is this an example of a violation of the SRP-principle (SOLID)? Should I extract "deployment responsibility" to another class?
like image 382
Hopeless Avatar asked Oct 13 '15 22:10

Hopeless


People also ask

Can static class have non static field?

Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it's explicitly passed in a method parameter.

Can we declare static variable in non static class in C#?

C# fields must be declared inside a class. However, if we declare a method or a field as static, we can call the method or access the field by using the name of the class. No instance is required. We can also use the static keyword when defining a field.

Should static fields be public?

Public static fields are useful when you want a field to exist only once per class, not on every class instance you create. This is useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.

Can static method access non static fields C#?

Static and Non-Static Methods in C# If you want to consume any non-static members with a static method then you need to create an object and then through the object, you can access the non-static members.

What are private static fields and methods?

Like their public equivalent, private static methods are called on the class itself, not instances of the class. Like private static fields, they are only accessible from inside the class declaration. Private static methods may be generator, async, and async generator functions. This can lead to unexpected behavior when using this.

What is the difference between static and non static fields?

Difference between Static and Non-Static fields of a class. Static variables or fields belong to the class, and not to any object of the class. A static variable is initialized when the class is loaded at runtime. Non-static fields are instance fields of an object.

How do you access the members of a static class?

Because there is no instance variable, we access the members of a static class by using the class name itself. C# fields must be declared inside a class. However, if we declare a method or a field as static, we can call the method or access the field by using the name of the class. No instance is required.

What are non-static fields in C++?

Non-static fields are instance fields of an object. They can only be accessed or invoked through an object reference. The value of static variable remains constant throughout the class.


Video Answer


1 Answers

It's bad that I'm using a static field?

It depends on what you're using it for. In this case since you change it with a non-static method (Deploy()), then yes, it's probably bad. If it should be the same for all instances, then set it in a static constructor or property (assuming that the application configuration will set it).

Is this an example of violation of SRP-principle (SOLID)?

What are the responsibilities of this class? Can you extract Deploy and Start logically or does one require the other?

like image 63
D Stanley Avatar answered Oct 23 '22 22:10

D Stanley