6.6. Writing Instance Methods

In Unit 5 you saw how to write static methods. A static method is also referred to as a class method, and it is called without an object. Because of this, static methods can’t access y attributes (instance variables) or other non-static methods without first creating an object.

Non-static methods, which we will refer to as instance methods or object methods are called using an object and therefore have access to an object’s instance variables. Note the method header will not include the keyword static.

There are three steps to creating and calling an instance method:

  1. Object of the Class: Declare an object of your class in the main method or from outside the class.

    // Step 1: declare an object in main or from outside the class
    Classname objectName = new Classname();
    
  2. Method Definition: write the method’s header and body code like below:

    // Step 3: Define the method in the class
    // method header
    public void methodName()
    {
          // method body for the code
    }
    
  3. Method Call: whenever you want to use the method, call objectName.methodName();

    // Step 2: call the object's method
    objectName.methodName(); //Step 2
    

How is this different than calling a static method? Notice the object reference and dot notation before the method name. Since the method may set and get instance variables, you must call the method on an actual instance of the class.

6.6.1. Practice

6.6.2. Summary

  • Procedural Abstraction (creating methods) reduces the complexity and repetition of code. We can name a block of code as a method and call it whenever we need it, abstracting away the details of how it works.

  • A programmer breaks down a large problem into smaller subproblems by creating methods to solve each individual subproblem.

  • To write methods, write a method definition with a method signature like “public void chorus()” and a method body in {} and method calls using an object.the method name and arguments whenever you need it to do its job.

  • To call an object’s method, you must use the object name and the dot (.) operator followed by the method name, for example object.method();

  • When you call a method, you can give or pass in arguments or actual parameters to it inside the parentheses object.method(arguments). The arguments are saved in local formal parameter variables that are declared in the method header, for example: public void method(type param1, type param2) { … }.

  • Values provided in the arguments in a method call need to correspond to the order and type of the parameters in the method signature.

  • When an actual parameter is a primitive value, the formal parameter is initialized with a copy of that value. Changes to the formal parameter have no effect on the corresponding actual parameter.

  • When an actual parameter is a reference to an object, the formal parameter is initialized with a copy of that reference, not a copy of the object. The formal parameter and the actual parameter are then aliases, both refering to the same object.

  • When an actual parameter is a reference to an object, the method or constructor could use this reference to alter the state of the original object. However, it is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.

You have attempted of activities on this page