user2224583
user2224583

Reputation: 79

Calling 'new' each time you want to use a function from a class

Is it okay to write a class like this:

public class AlexaApi
{
    public string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

But then when I use the class, call "new" each Time I want to access its functions.

new AlexaApi().ResponseBuilder(new AlexaApi().CreateRandomPhrase());

I understand that this creates two instances of the AlexaApi object and it most likely should be written like:

var alexa = new AlexaApi();
alexa.ResponseBuilder(alexa.CreateRandomPhrase());

Would it use up more memory calling "new" each time? What is the worst thing that can happen? Is it really bad form to do what is written in the first example?

Upvotes: 0

Views: 83

Answers (4)

Liam
Liam

Reputation: 29752

There's a fair chance that what you actually want is a static method:

public class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

you can then call these methods thus:

AlexaApi.CreateRandomPhrase();
..etc.

i.e. you avoid instanciating a new class each time.

But be aware that static methods and classes have slightly different symantics to none static ones. Particuarly

  • You can't store state in the class
  • There is only ever one instance of the method in memory (this could be memory efficient but it can also lead to memory leaks if your not careful)
  • static objects can be an issue in unit testing as they cannot be injected in

You don't show details for what this class does so it's hard to be explicit. If you methods are just "logic", then there is no harm in making them static. If they load resources, etc. then don't.

For more info see Static vs non-static class members

Also note a class that only contains static methods can itself be made static. This just prevents you declaring none static methods/properties

public static class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

Upvotes: 2

Aaron
Aaron

Reputation: 146

As you mentioned in your question, using new instances of the class each time will take up more memory. Depending on the scope where you're calling this method and the size of the class (how many instance variables, not lines of code) this practically could amount to almost no extra memory (ram storing the extra instance of the class) or CPU time (creating the class with any default values/constructors you have in place) to eating up more memory than the rest of your program combined.

As mentioned in the comments and an answer that was posted while I was typing this, you could always make those methods static, which means they can be called without using instances of the class at all and will save you the trouble.

Upvotes: 0

Antoine V
Antoine V

Reputation: 7204

keyword static should fit your case:

public class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

and call directly the method without creating instance :

AlexaApi.ResponseBuilder(AlexaApi.CreateRandomPhrase());

Upvotes: 0

Fabulous
Fabulous

Reputation: 2423

To do all that without creating multiple instances you should static methods as has been suggested. Consider the following code:

public class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

This way you can get the same results using code such as:

AlexaApi.ResponseBuilder(AlexaApi.CreateRandomPhrase());

Upvotes: 0

Related Questions