Skip to main content
Engineering LibreTexts

12.3: Adding New Methods

  • Page ID
    15230
  • You have probably guessed by now that you can define more than one method in a class. Here’s an example:

    public class NewLine {
    
        public static void newLine() {
            System.out.println();
        }
    
        public static void main(String[] args) {
            System.out.println("First line.");
            newLine();
            System.out.println("Second line.");
        }
    }
    

    The name of the class is NewLine. By convention, class names begin with a capital letter. NewLine contains two methods, newLine and main. Remember that Java is case-sensitive, so NewLine and newLine are not the same.

    Method names should begin with a lowercase letter and use “camel case”, which is a cute name for jammingWordsTogetherLikeThis. You can use any name you want for methods, except main or any of the Java keywords.

    newLine and main are public, which means they can be invoked from other classes. They are both static, but we can’t explain what that means yet. And they are both void, which means that they don’t yield a result (unlike the Math methods, for example).

    The parentheses after the method name contain a list of variables, called parameters, where the method stores its arguments. main has a single parameter, called args, which has type String[]. That means that whoever invokes main must provide an array of strings (we’ll get to arrays in a later chapter).

    Since newLine has no parameters, it requires no arguments, as shown when it is invoked in main. And because newLine is in the same class as main, we don’t have to specify the class name.

    The output of this program is:

    First line.
    
    Second line.
    

    Notice the extra space between the lines. If we wanted more space between them, we could invoke the same method repeatedly:

    public static void main(String[] args) {
        System.out.println("First line.");
        newLine();
        newLine();
        newLine();
        System.out.println("Second line.");
    }
    

    Or we could write a new method that displays three blank lines:

    public static void threeLine() {
        newLine();
        newLine();
        newLine();
    }
    
    public static void main(String[] args) {
        System.out.println("First line.");
        threeLine();
        System.out.println("Second line.");
    }
    

    You can invoke the same method more than once, and you can have one method invoke another. In this example, main invokes threeLine, and threeLine invokes newLine.

    Beginners often wonder why it is worth the trouble to create new methods. There are many reasons, but this example demonstrates a few of them:

    • Creating a new method gives you an opportunity to give a name to a group of statements, which makes code easier to read and understand.
    • Introducing new methods can make a program smaller by eliminating repetitive code. For example, to display nine consecutive new lines, you could invoke threeLine three times.
    • A common problem solving technique is to break tasks down into sub-problems. Methods allow you to focus on each sub-problem in isolation, and then compose them into a complete solution.
    • Was this article helpful?