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)
{
}
-
1This 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– Theomax2013年10月26日 10:37:23 +00:00Commented Oct 26, 2013 at 10:37
-
@Theomax brilliant! I was unclear, as I am no programmer (yet) and studyinguser208372– user2083722013年10月26日 10:39:35 +00:00Commented Oct 26, 2013 at 10:39
1 Answer 1
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).
-
I was wondering about that, but what about
main
??user208372– user2083722013年10月26日 10:35:01 +00:00Commented Oct 26, 2013 at 10:35 -
public static void main(String[] args)user208372– user2083722013年10月26日 10:35:38 +00:00Commented 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.Oded– Oded2013年10月26日 10:36:56 +00:00Commented 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)Oded– Oded2013年10月26日 10:40:13 +00:00Commented Oct 26, 2013 at 10:40