DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 3.14 Creating a Constructor

3.14.1 Problem

You want Eclipse to add a constructor to a class, including a call to the superclass's constructor.

3.14.2 Solution

Select Source Add Constructor from Superclass.

3.14.3 Discussion

For example, if you have this code:

public class DisplayApp {

    static String text = "No problem.";
    
    public static void main(String[] args)
    {
        System.out.println(text);
    }
}

and you select Source Add Constructor from Superclass, Eclipse will give you this:

public class DisplayApp {

    static String text = "No problem.";
    
    /**
     * 
     */
    public DisplayApp( ) {
        super( );
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args)
    {
        System.out.println(text);
    }
}

You also can create constructors automatically when you create a class.


3.14.3.1 Eclipse 3.0

In Eclipse 3.0, you can create a constructor that will assign values to one or more fields. Select Source Generate Constructor using Fields, opening the dialog shown in Figure 3-15.

Figure 3-15. Creating a constructor that will fill fields
figs/ecb_0315.gif


Selecting the two String fields, text and message, creates this constructor:

public DisplayApp(String text, String message) {
    super( );
    this.text = text;
    this.message = message;
}

3.14.4 See Also

Chapter 2 of Eclipse (O'Reilly).

    Previous Section  < Day Day Up >  Next Section