Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

[Question] Java: calling static methods - best practice

Options
  • 12-11-2013 2:41pm
    #1
    Closed Accounts Posts: 310 ✭✭


    Hi,

    This is definitely a beginners question: Say I have a class that contains a main method and a number of other static methods The purpose of the class is to just read some files and based on their content calling a factory class that will create different implementations of an interface

    The static methods that I have written are all short with a specific purpose and in main I am calling them one by one passing the returned object or collection from one to the next method and so on.

    My question is, is this bad practice? It means that main looks like a bit like a sequence of method calls. Should I instead be calling methods from other methods rather than returning to main each time?

    Thanks


Comments

  • Registered Users Posts: 2,815 ✭✭✭SimonTemplar


    Annuv wrote: »
    Hi,

    This is definitely a beginners question: Say I have a class that contains a main method and a number of other static methods The purpose of the class is to just read some files and based on their content calling a factory class that will create different implementations of an interface

    The static methods that I have written are all short with a specific purpose and in main I am calling them one by one passing the returned object or collection from one to the next method and so on.

    My question is, is this bad practice? It means that main looks like a bit like a sequence of method calls. Should I instead be calling methods from other methods rather than returning to main each time?

    Thanks

    I'm by no means an advanced Java programmer but my guess is that your above methodology improves readability/maintainability and it is also dependency injection so calling multiple methods from a controlling method is the better of the two options. There is nothing wrong with a method simply containing other method calls.

    More experienced programmers may disagree though. I'd be interested to see their opinions also.


  • Registered Users Posts: 27,123 ✭✭✭✭GreeBo


    If you always call the same methods, why not have the first one call the next one, and continue in that manner?
    If the static methods make no sense on their own, they should all probably be private bar the entry method?

    For me, it kinda depends on what you are doing, is this all the code there is for example?

    You are kind of using the Template Pattern, you call a method (in your case main) and it executes a bunch of other methods to get your result.
    Template Pattern

    Also, you should think about creating a new object and returning that rather than modifying the input object from each method, immutability makes life easier in the long run.


  • Registered Users Posts: 7,031 ✭✭✭witnessmenow


    On the calling methods from other methods thing.

    If each of your methods have a defined function(purpose) they should not call each other. If when run together they have a separate function, then a method that calls both should be created.

    Say for a really basic example I had a camel case converter I might have the following methods

    string makeOnlyFirstLetterCapital(string input)

    string removeSpacesBetweenWords(string input)

    each of them on their own have a very clear function. so rather than calling one from the other just create a separate method

    string convertToCamelCase(string input)

    that calls both

    So should your main have a sequence of method calls, I would think that is ok. As above I would try group methods calls into other methods where it makes sense, but if it doesn't then that's fine too.

    Just a question on why you are using statics? If you are passing an object into a series of static methods does it make more sense to have a class that contains the properties the methods need and not use static methods, so call the methods on the instance of your object instead? Its hard to advise without context but worth considering


  • Closed Accounts Posts: 310 ✭✭Annuv


    Thanks lads for all the suggestions, sorry that I'm only replying now. After reading your replies I think the approach I have taken is a bit misguided. Let me describe in more detail what I'm attempting to do with an example

    Say I have a certain file type that contains text that describes a 'system', information such as hostname, system number etc. If there are two possible system types, system type A and system type B, where system type B has all the features of A and more, and this is reflected in the data contained in the files

    These files are located in a certain file system directory. Let's say there are two files describing systems of type B and one of type A. My intention is to read data from those files and then create instances of a system class that I will write based on that data
    So maybe I'll create a system interface and have systemA and systemB implement it or have systemB extend system A for example

    Before this system objects can be instantiated, there is a significant amount of data that must be read from the system files. At the moment I am doing that in a class containing main and to create the 'system' objects based on that data, I was thinking of creating a factory class to create the system instances, which i would call from main or I could instantiate the systems directly from the class containing main

    What I have done, that has lead to my question in this thread, is taken a lot of the file reading and parsing from within main and put it into static methods within the same class that contains the main method. Each of these has clear purpose, for example retrieving a certain key,value pair from the files.

    My intention here was to make the code a little tidier and more readable but I'm not sure if this makes sense. This class will never be instantiated from another class so as GreeBo stated maybe they should be all private anyway.

    So what do you think? I don't know if I should have written another class specifically for reading the system files, since this is only done once per program run


    Thanks


  • Registered Users Posts: 2,020 ✭✭✭Colonel Panic


    There's no right answer, but here's what I do: I favour a large Init function or method to set things up and pass all those to the "system" object be it an instance of an Application class, some Service or whatever.

    Don't overthink it, it's one shot code and if it's all in one method, you can see all your dependencies in one place.

    If it gets too big, or you're repeating code, think about refactoring things into convenience methods like ParseCommandLine, CheckPermissions, or LoadSettings or whatever. In C or C++ these would just be functions but in C# and Java you don't have that luxury and a static method is close enough.


  • Advertisement
Advertisement