tags:

views:

171

answers:

7

When I'm working on homework assignments for class it is typically beneficial if the programs are small to keep the problem to a single class (easy to submit with our script).

For these one-off programs does it make more sense to declare all methods and instance variables as static or does it make more sense to declare an instance of the class itself in the main method?

So, for example:

A.

public class MergeSort
{
   public static Scanner input = new Scanner(System.in);

    public static void main(String[] args)
    {
        int[] randData = getRandomData();
        int[] sortedData = mergeSort(randData);
    }

    public static int[] mergeSort(int[] unsortedData)
    {
        // ...
    }

    public static int[] merge(int[] data, int a, int b, int c)
    {
        // ...
    }
}

vs

B.

public class MergeSort  
{  
   public Scanner input = new Scanner(System.in);

    public static void main(String[] args)
    {
        MergeSort ms = new MergeSort();
        int[] randData = ms.getRandomData();
        int[] sortedData = ms.mergeSort(randData);
    }

    public int[] mergeSort(int[] unsortedData)
    {
        // ...
    }

    public int[] merge(int[] data, int a, int b, int c)
    {
        // ...
    }
}
+11  A: 

Personally I'd go with B, it's a good habit to get into.

Binary Worrier
and "everything is static" is a bad habit which should be avoided
matt b
+1 for B choice
Pascal Thivent
A: 

static should be only used when you want to use the method without needing a class instance. The static methods should also be independent from any instance variable (only use local or static variables)

Carlos Tasada
In fact, static methods MUST be independent from instance variables - the compiler won't let you define them otherwise.
Andrzej Doyle
A: 

this is really a matter of style. it's usually easier to instantiate inside main because then you don't need to keep mentioning everything as static later on.

Omry
+4  A: 

I would make those methods static since they don't depend on the state of the object in any way. After all the sort methods in the Arrays and Collections classes are static too.

sepp2k
The insight here is that java may confuse beginners by requiring that all functions(methods) be included in a class, even if none of the features of object oriented (OO) design be required. OO is a good thing and java does a good job at supporting it, but it is not warranted everywhere...
mjv
A better approach yet, is to leave them static, but _in a separate class_, allowing "self documentation" and avoiding namespace conflicts, and yet respecting the "no instance needed" nature of the methods.for example from main() you'd call Assignement12.GetRandomData() or some other class name if one fit (say SortingLib or even Utils)
mjv
A: 

For homework, it's really a matter of taste. For real projects, you should instantiate the class and wrap that in try/catch so you can print the full stack trace if something goes wrong during construction of the class.

The default printStackTrace() of Java sometimes omits the nested exceptions, so you'll end up with "there was an error, see next exception" but you can't see it.

Aaron Digulla
+1  A: 

For homework, I would make it a habit of doing everything "by the book". If you don't, you may pick up bad habits and introduce bugs into your code in the future when working on more complicated assignments/projects. Good practice starts early and really shines through as you develop your skills in whatever language (it's what makes sometimes makes the difference between an 'ok' programmer and a 'good' programmer, in my opinion).

I associate it with any other subject in school; if you were learning English (for example) it makes sense to properly spell words, rather than replace them with say internet acronyms.

Malaxeur
Can you clarify what "by the book" means in your answer? It's never specified, and I think that's the really the point of this question.Also, I think the better English analogy would be active vs. passive voice. It's not really a question of being correct, but a question of style/design choice.
Carl
"By the book" is really just following the standard Object-Oriented paradigm as much as you can. In this particular case, the methods (mergeSort) do not define the behaviour of a particular object therefore I'd highly recommend the methods being static. They truly don't belong to specific instances of the class, but rather to the class as a whole. (Although... all methods belonging to a class definitely has its drawbacks here...) In other words, you shouldn't need a MergeSort object to sort :PI see it less of a style/design choice but more of a "writing code that others can understand".
Malaxeur
+1  A: 

If you are in fact using the class as a collection of algorithms/methods that act on external data without reference to any fields that are part of a specific instantiation of the class, then yes, the methods should be static. That's the point of static methods.

However, there a lot of nuances that make several of the existing answers (i.e., don't use static methods) the better pragmatic advice. For example, if these classes might be used later as a particular instance of a Strategy interface/abstract class, all the static markers would have to be removed. Of course, it's all advice - both A and B are suitable answers.

If your instructor says only one is correct, then s/he is enforcing other requirements (e.g., coding standards to support later refactoring) beyond the here-and-now design requirement. Those other requirements are also a learning opportunity, so make sure the instructor explains the wherefores.

Carl