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) 
{
}
gnat
20.5k29 gold badges117 silver badges308 bronze badges
asked Oct 26, 2013 at 10:29
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. Commented Oct 26, 2013 at 10:37
  • @Theomax brilliant! I was unclear, as I am no programmer (yet) and studying Commented Oct 26, 2013 at 10:39

1 Answer 1

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).

answered Oct 26, 2013 at 10:34
4
  • I was wondering about that, but what about main ?? Commented Oct 26, 2013 at 10:35
  • public static void main(String[] args) Commented Oct 26, 2013 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. Commented Oct 26, 2013 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) Commented Oct 26, 2013 at 10:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.