JAVA E-NEWSLETTER for March 18, 2004 <---------------Advertisement---------------> MAXIMIZE SQL SERVER - LEARN STEP-BY-STEP How will Microsoft SQL Server help you streamline costs and enhance productivity? With 87 complete lessons, TechRepublic's Fast Track: Microsoft SQL Server 7.0/2000 CD-ROM helps you develop the skills and knowledge needed to program, administer, back up, and optimize SQL Server 7.0 and 2000. Learn at your own pace, no matter what your SQL skill level. From the basics of programming to advance querying, you'll have the know-how and expertise to enjoy SQL's scalability and reliability. http://ct.com.com/click?q=be-B7ZxQCp_Zl7gNWLcKSrsvjNmgH78 <-------------------------------------------> CREATING METHODS THAT ACCEPT A VARIABLE NUMBER OF ARGUMENTS Starting with Java 1.5, you can create methods that accept a variable number of arguments. Like many of the other enhancements in Java 1.5, this is purely a syntax change. The syntax for creating a method that takes a variable number of arguments looks like this: public void foo(String ... names) {} Calling a method that takes a variable number of arguments is similar to calling a method that takes a predefined number of arguments: foo("joe", "mandy"); or foo("joe"); or foo("joe", "mandy", "david"); Behind the scenes, the compiler is creating an array (i.e., a String array), loading your variables into it, and passing the array as the argument. To confirm this, the following snippet calls the method with a String array as the sole argument. All of the code compiles and works as expected. foo(new String []{"joe", "mandy", "david")); Variable argument methods also work with primitives, as the snippet below illustrates: public void bar(int ... vals) {} ... bar(3, 9, 100); The code below contains two examples of methods that accept a variable number of arguments. Play with it to see if it works as you expect. public class VarArgsTip { public static void main(String []args) { foo("one", "two", "three"); foo(new String []{"one", "two", "three"}); bar(1, 2, 45, 101); } static void foo(String ... names) { for (String s: names) { System.out.println(s); } } static void bar(int ... vals) { for (int num : vals) { System.out.println(num); } } } NOTE: The code in this tip was compiled on Windows 2000 using Java build 1.5.0-beta-b32c. To compile the code in this tip, you must use the javac "-source 1.5" option. WHAT DO YOU THINK ABOUT THIS FEATURE? Some programmers say this feature will promote bad design because it encourages the use of methods that take an unknown number of arguments and blurs the line even further between method arguments and composite objects. They're probably right. Some people will abuse the feature or fail to recognize when an object is a better solution than an unknown number of objects; however, I believe that the net change is still a positive one. If you want to share your thoughts about this feature, send us your feedback. (Please include "Java: Variable method arguments in the subject line of your e-mail.) mailto:Enews2@cnet.com David Petersheim is the Director of Application Development with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ----------------------------------------