Beruflich Dokumente
Kultur Dokumente
New Java language features Using new to create an object. eferencing fields using dot notation.
$ class defines what fields an object will have when it's created. +hen a new Student, object is created# a bloc% of !e!ory is allocated# which is big enough to hold these three fields -- as well as so!e e.tra overhead that all objects have.
Student1 tatiana tatiana # new Student1$% // &reate Student1 object wit' new.
//... &reate a new Student1 object for )atiana. tatiana # new Student1$% // &reate a new Student1 object wit' default values. tatiana.firstName # *)atiana* // Set values of t'e fields. tatiana.lastName # *+o'nson* tatiana.id # ,,-./01 +2ptionPane.s'ow3essage(ialog$null4 *2ne student is named: * 5 tatiana.lastName 5 *4 * 5 tatiana.firstName%
Awkward? "his si!ple e.a!ple with one student is so!ewhat aw%ward# but we'll get to e.a!ples that show real advantages soon.
import java6.swing.7
8 0 9 : /
public class )estStudent1 { public static void main$String;< args% { Student1 tatiana Student1 pupil //... &reate new Student1 object wit' new. tatiana # new Student1$% tatiana.firstName # *)atiana* tatiana.lastName # *+o'nson* tatiana.id # ,,-./01
//... &reate anot'er Student1 object. pupil # new Student1$% , pupil.firstName # +2ptionPane.s'owInput(ialog$null4 *First name*% pupil.lastName # +2ptionPane.s'owInput(ialog$null4 *!ast name*% 1. pupil.id # Integer.parseInt$+2ptionPane.s'owInput(ialog$null4 *I(*%% 11 +2ptionPane.s'ow3essage(ialog$null4 *2ne student is named: * 11 5 tatiana.lastName 5 *4 * 5 tatiana.firstName 5 *=n and anot'er is named: * 18 5 pupil.lastName 5 *4 * 5 pupil.firstName% " 10 " 119 1: 1/ 1, 1. 11 11 18 10 119 1:
Purpose of this lesson: "o introduce the concept of constructors. 2urpose of constructors is both guaranteed initiali1ation and convenience. New Java language features Constructor synta. - li%e a !ethod but with class na!e and i!plicit type3value.
Constructor S(nta)
Si"ilar to "ethod. $ constructor is si!ilar to a !ethod -- it's called li%e a !ethod# has para!eters li%e a !ethod# and it returns. 5ut it !ust have the sa!e na!e as the class for which it is a constructor. $lso# the type and return value are i!plicit.
//######################################## constructor public Student1$String fn4 String ln4 int idnum% { firstName # fn lastName # ln id # idnum " "
6ote that a constructor has no return type and the na!e is the sa!e as the class na!e.
Here is the first progra! rewritten to use the above class definition with a constructor.
1 // File : oop/dataclass/)estStudent1.java 1 // Purpose: )ests Student1 constructor. 8 0 import java6.swing.7 9 public class )estStudent1 { : public static void main$String;< args% { / Student1 tatiana , Student1 pupil 1. 11 //... &reate new Student1 object wit' new. 11 tatiana # new Student1$*)atiana*4 *+o'nson*4 ,,-./01% 18 10 //... &reate anot'er Student1 object. 1String first # +2ptionPane.s'owInput(ialog$null4 *First name*% 19 String last # +2ptionPane.s'owInput(ialog$null4 *!ast name*% 1: int studI(# 1/ Integer.parseInt$+2ptionPane.s'owInput(ialog$null4 *I(*%% 1, pupil # new Student1$first4 last4 studI(% 1. 11 +2ptionPane.s'ow3essage(ialog$null4 *2ne student is named: * 11 5 tatiana.lastName 5 *4 * 5 tatiana.firstName 18 5 *=n and anot'er is named: * 10 5 pupil.lastName 5 *4 * 5 pupil.firstName% 1" "
"he constructor can chec% that all fields are defined with legal values. +e're not going to e.tend the Student8 class any further to test for legal values# but we will in the ne.t e.a!ple. *or the !o!ent we'll leave the Student class# and !ove to so!ething different to show the sa!e ideas.