3.14. Creating a Constructor
Problem
You want Eclipse to add a constructor to a class, including a call to the superclass’s constructor.
Solution
Select Source→ Add Constructor from Superclass.
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); } }
Tip
You also can create constructors automatically when you create a class.
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
Selecting the two String
fields,
text
and message
, creates this
constructor:
public DisplayApp(String text, String message) { super( ); this.text = text; this.message = message; }
See Also
Chapter 2 of Eclipse (O’Reilly).
Get Eclipse Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.