1

When creating constructors with multiple arguments, is ok to use the generic String[] args or is it better to list the arguments? Does it really make any significant difference as the coding becomes more complicated?

public static void Something (String[] args) 
{
}

or

public static void Something (String lots, ... int some, ... etc) 
{
}
2
  • 1
    This question is relevant as it relates to best practices and even one of the solid principles as highlighted by Oded's answer. Plus it's good you are taking enough care to cover the basics in detail. – Theomax Oct 26 '13 at 10:37
  • @Theomax brilliant! I was unclear, as I am no programmer (yet) and studying – user208372 Oct 26 '13 at 10:39
4

A String[] is suitable if your class has a single String[] that you need to construct it with.

It is not suitable if you need different types - how would you represent an int in a String[]? You would need some sort of convention and this would be prone to error and be overly complex to boot.

Use the right types for the class. It is normally OK to have several parameters for the class, but if there are 7 or more (rule of thumb - don't take this as gospel) it is probably doing too much (see Single Responsibility Principle).

4
  • I was wondering about that, but what about main ?? – user208372 Oct 26 '13 at 10:35
  • public static void main(String[] args) – user208372 Oct 26 '13 at 10:35
  • 3
    @Skippy - main is special. It is the entry point to the executable and has to be there and in that manner (if you want to pass arguments to it from the command line). It is not best practice, but practically speaking, there is no alternative. – Oded Oct 26 '13 at 10:36
  • 2
    @Skippy - We all started somewhere, though my answer is general enough to be correct with other OOP languages (C# for example, which I am much more familiar with than Java) – Oded Oct 26 '13 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy