{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part17

Intro to Java Web-Notes_Part17 - public class ShowMethods4...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: public class ShowMethods4 { public static void main(String args) { Demo dm = new Demo(); dm.first(); } } class Demo { void first( ) { sample(); statementB; statement6; statement7; statement8; statement9; } void sample() { statementl; statementZ; statement3; statement4; } Putting a group of statements into a separate method is especially useful if there are groups of repeated statements. In the example below, the statements 1 through 4 are repeated three times. public class ShowMethods5 { public static void main(String args) { Demo dm = new Demo(); dm.first(); } } class Demo { void firs:( ) { :a:eme :a:eme :a:eme :a:eme (BLOC/)0) »—J»—J»—J»—> U) :a :eme U) o l | :a :eme o l | :a :eme :a :eme :a :eme MIDI/)0) »—>»—>»—>»—J :a :eme U) :a :eme :a :eme S :a :emea: ,' o (l \l .~. U) o l l ‘ s:a:emeq:1; statementZ; statementB; statement4; } Below, the repeated statements are pulled in to a separate method named common and written down once in that method. The common method is then called three times: public class ShowMethods { public static void main(String args) { Demo dm : new Demo(); dm.first(); } class Demo { void first( ) { common(); statement5; statement6; common(); statement7; statement8; statement9; common(); } void common() { statementl; statementZ; statement3; statement4; Having only one written copy of the repeated set of statements is especially useful if the statements occasionally need to be modified. Passing parameters You can pass values in to a method. A value passed to a method is referred to as a "parameter". public class ShowMethods6 { public static void main(String args) { Demo dm = new Demo(); dm.first(); } class Demo { void first() { doublelt(l67); // here we are "calling" the doublelt method ...
View Full Document

{[ snackBarMessage ]}