< Day Day Up > |
Recipe 3.14 Creating a Constructor3.14.1 ProblemYou want Eclipse to add a constructor to a class, including a call to the superclass's constructor. 3.14.2 SolutionSelect Source Add Constructor from Superclass. 3.14.3 DiscussionFor 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); } }
3.14.3.1 Eclipse 3.0In 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 fieldsSelecting 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 AlsoChapter 2 of Eclipse (O'Reilly). |
< Day Day Up > |