This chapter reviews method parameters and local variables, as well as method overloading and method signature. Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods. The name together with the number and types of a method's parameter list is called the signature of a method. The return type itself is not part of the signature of a method.
Colors in the following show how this works. this is used in the constructor where the instance variable, not the parameter, should be used. class Mystery{ private int sum; public Mystery( int sum ) this.sum = sum; sum = sum + inc; int sum = 99; System.out.println("sum: " + sum ); Now look at this modified version: class Mystery{ private int sum; public Mystery( int x ) { sum = x; } public void increment( int inc ) { sum = sum + inc; System.out.println("Mystery sum: " + sum ); }}public class Tester{ public static void main ( String[] args) { Mystery mystA = new Mystery( 34 ); Mystery mystB = new Mystery( 13 ); mystA.increment( 6 ); mystB.increment( 7 ); } } Page 2
Learn new skills or earn credit towards a degree at your own pace with no deadlines, using free courses from Saylor Academy. We're committed to removing barriers to education and helping you build essential skills to advance your career goals. Start learning here, or check out our full course catalog. Log in or Sign up to enroll in courses, track your progress, gain access to final exams, and get a free certificate of completion!
Page 3
Learn new skills or earn credit towards a degree at your own pace with no deadlines, using free courses from Saylor Academy. We're committed to removing barriers to education and helping you build essential skills to advance your career goals. Start learning here, or check out our full course catalog. Log in or Sign up to enroll in courses, track your progress, gain access to final exams, and get a free certificate of completion! Page 4
Learn new skills or earn credit towards a degree at your own pace, with no deadlines, using free courses from Saylor Academy. We're committed to removing barriers to education and helping you build essential skills to advance your career goals. Choose a course below, or check out our full course catalog. Log in or Sign up to enroll in courses, track your progress, gain access to final exams, and get a free certificate of completion! |