Notes on Chapter Three Methods 1. A Method is a named block of code that can be executed by using the method name. When the code in the method has completed it will return to the place it was called in the program. 2. Methods are called by inserting the method name followed by a pair of () symbols. This indicates to Java that the name references a method. 3. Methods are created outside of main() and any other methods but inside the class block, which is defined by { } symbols. Methods can be placed before or after the location from which they are called.
4. Method have two parts, a title and a body. The body is enclosed in { } symbols. Methods that appear with main() must have the leading identifier static because main() is a static method. 5. Method names must follow the same rules as variable naming. That is, case must match and method names cannot use reserved words, have spaces, special characters, etc. 6. By convention method names start with a lower case letter and capitalize other letters that makeup words. 7. Once the code inside a method completes the program transfers control back to the place in the code from where the method was called. 8. Access specifiers are words placed before the method return type that indicate how the method will be handled within the class. The terms that define visibility within a class include: public can be executed from a class object type private can only be executed from within the class protected can only be executed from within the class or by any inherited classes The term that defines class level access is static which means that the method must be executed at the class level and does not require objects.
You will learn more about these modifiers later. 9. Modifiers are placed before the method return type, which is placed just before the method name. In this case, the method returns an integer which is the data type of the item being returned. 10. If a method does not return a value then the return type is void. 11. Method automatically jump back to where they were called but in order to return something from a method the items being returned must appear after the statement return x where x is either a variable or a value. 12. When a method is called it must be followed by a pair of () symbols. To pass information into the method you would list the variables or values inside these () characters and separate the list with commas:
The method named gettotal has three arguments or parameters. When the method is called (line 13) two variables and a number are passed into the method. 13. When a method that uses parameters is called, the data in the parameters is copied into the parameters defined for the method. In the previous sample the contents of val1 is copied into v1, the contents of val2 is copied into v2, and the number 500 is copied into v3. The number of parameters used when calling the method must match the number of variables defined for the method. 14. When writing a method the data type of the method parameters must be defined. This identifies to Java what kind of data to expect in the variables defined for the method (line 18).
15. The parameters, once defined for the method, can be used inside the method as regular variables. However, the scope of the variables defined inside the method are tied to the method body. That is, you cannot use variable declared inside a method outside the method. You can also not use variables defined in the calling method (in this case main() ) inside the method.
16. Methods can be called from other methods. 17. Methods can be overloaded which means that different methods can have the same name but must have different parameter types:
18. Overloaded methods must differ in parameters. Differing in return types is not enough. Classes and Object 1. Classes are software constructions that are used to organize and design programs. They are also called Abstract Data Types (ADT) because they are used like regular data type to define what look like variables but are called Objects. 2. Classes can include both methods and properties. Methods are functions that either the Class or the Objects defined from the class use. Properties are data objects that are defined from either fundamental data types or from other classes. 3. Properties and Methods in classes have an access type. The access types will either be public, private, or protected. 4. Classes can be defined with the class name followed by the block characters { }. The class name must follow the variable naming conventions.
5. Classes must be defined before they can be used. They usually are defined outside other classes, but you can create inner classes that are defined inside other classes. 6. Most classes will have both Properties and Methods. By convention Properties are defined to be private. Methods used to access the properties are defined to be public. 7. Most methods will have a getter and setter method. The getter method returns the private property and the setter method sets the private property.
8. The property and methods inside a class must be set with the access modifier public, private or protected. 9. Private properties and methods cannot be accessed from objects but only by other methods inside the class. Public properties can be accessed from objects. 10. Using classes normally requires that an instance of the class be defined. This is done by using the class as a data type and the defined instance is an object.
11. Once objects are defined they each hold separate instances of the properties and method defined in the class. Only public properties and methods can be accessed by using the object name, a period, and the method or property. 12. Before using an object it must be created by using the command new ClassName(). Once defined the public methods and properties can be accessed from the object.
13. The new command will create the object instance in memory. 14. Regular methods defined inside a class are called non-static methods because they can only be used once you have created an object from the class using the new method.
15. A Constructor method is a special method which is used when an object is created using the new method. These methods don t need a return type and will be run when a new object is defined with the new command:
16. Constructor methods can perform multiple tasks. 17. Just like regular methods, Class methods can be overloaded which means that different parameters will define different methods.
18. Classes can have object from other classes: package samplemethod; class EmpPay { private double payrate; private int hours; } public void setpayrate(double p) { payrate=p; } public void sethours(int h) { hours = h; } public double getpayrate() { return payrate; } public int gethours() { return hours; } public double calcpay() { return payrate * hours; } class Employee { private String name; private EmpPay emp; public Employee(String n) { emp = new EmpPay(); name=n; emp.sethours(40); emp.setpayrate(10.00); } public void setname(string n) { name=n; } public void setemppay( EmpPay e) { emp = e; } public EmpPay getemppay() { return emp; } public void setpayrate(double pr) { emp.setpayrate(pr); } public void setpayrate(string pr) { emp.setpayrate( Double.parseDouble(pr)); } public void sethours(int h) { emp.sethours(h) } public void sethours(string h) { emp.sethours(integer.parseint(h); } public String getname() { return name; } public double getpayrate() { return emp.getpayrate() } public int gethours() { return emp.gethours() } public double calcpay() { return emp.calcpay(); }
} public class samplemethod { public static void main(string[] args) { Employee e1, e2; e1 = new Employee("Joe Jonees"); e2 = new Employee("Sara Smith"); EmpPay emp1, emp2; emp1 = e1.getemppay(); emp2 = e1.getemppay(); System.out.println("Starting main"); } }