Saturday, November 26, 2011

User Defined Classes and ADT's

Do you want to know the secrets of Java Programming and use it for the advancement of your career in IT? Do you have problems in your Java course? Here is an instant solution for your problem. Avail the Java training package of Prof Erwin Globio and get free tips about Java Programming Secrets. For more free information about Java and Java Training visit the following sites:http://erwinglobio.sulit.com.ph/ http://erwinglobio.multiply.com/http://erwinglobio.wordpress.com/. You may contact the Java Trainer at 09393741359 or 09323956678. Call now and be one of the Java Experts.

Classes

A class is a collection of a fixed number of components.  The components of a class are called the members of the class.  The general syntax for defining a class is

modifier(s) class ClassIdentifier modifier(s)
{
classMembers
}

where modifier(s) are used to alter the behavior of the class.  ClassMembers generally consist of named constants, variable declarations and/or methods.  Some modifiers are public, private, and static.

  • If a member of a class is a named constant, you declare it just like any other named constant.

  • If a member of a class is a variable, you declare it just like any other variable.

  • If a member of a class is a method, you define it just like any other method.

  • If a member of a class is a method, it can (directly) access any member of the class—data members and methods. Therefore, when you write the definition of a method, you can directly access any data member of the class (without passing it as a parameter).

The word class is a reserved word in Java and it defines only a data type; no memory is allocated.

The data members of a class (also called fields) are classified into three categories: private, public and protected.  If a member of a class is private, you cannot access it outside the class.  If a member of a class is public, you can access it outside the class.

The (non-static) data members of a class are called instance variables.

The methods of a class are called the instance methods of the class.

Teaching Tip

If a class member is declared/defined without any modifiers, then that class member can be accessed from anywhere in the package.

Teaching Tip

It is good practice to make as many instance variables and methods private as is possible.  This is discussed later in this chapter.


Constructors

In addition to the methods necessary to implement operations, every class has a special type of method called constructors. A constructor has the same name as the class, and it executes automatically when an object of that class is created. Constructors are used to guarantee that the instance variables of the class initialize to specific values.

There are two types of constructors: with parameters and without parameters. The constructor without parameters is called the default constructor.

Constructors have the following properties:

  • The name of a constructor is the same as the name of the class.

  • A constructor has no type.

  • Constructors of a class can be overloaded.

  • Any two constructors must have different signatures.

  • Constructors execute automatically when class objects are instantiated. Because they have no types, they cannot be called like other methods.

  • If there are multiple constructors, the constructor that executes depends on the type of values passed to the class object when the class object is instantiated.

Teaching Tip

If you do not include any constructor in a class, then Java automatically provides the default constructor.


Unified Modeling Language Class Diagrams

A class and its members can be described graphically using Unified Modeling Language (UML) notation.  The top box in the UML diagram contains the name of the class. The middle box contains the data members and their data types. The last box contains the member method names, parameter list, and return types. The + (plus) sign in front of a member indicates that it is a public member; the – (minus) sign indicates that it is a private member. The # symbol before a member name indicates that it is a protected member.


Variable Declaration and Object Instantiation

Once a class is defined, you can declare reference variables of that class type.  In order to allocate memory space for the variable the operator new is used as follows:

new className()  

OR 

new className(argument1, argument2, …, argumentN)

The first statement instantiates the object and initializes the instance variables of the object using the default constructor.

The second statement instantiates the object and initializes the instance variables using a constructor with parameters.  The number of arguments and their type should match the formal parameters (in the order given) of one of the constructors.  If the type of the arguments does not match the formal parameters of any constructor (in the order given), Java uses type conversion and looks for the best match.


Accessing Class Members

Once an object is created, you can access the public members of the class. The general syntax to access a data member of a class object or a class method is:

referenceVariableName.memberName

The class members that the class object can access depend on where the object is created.

  • If the object is created in the definition of a method of the class, then the object can access both the public and private members.

  • If the object is created elsewhere then the object can access only the public members of the class.

The dot . (period) is called the member access operator.


Built-in Operations on Classes

Most of Java’s built-in operations do not apply to classes. The built-in operation that is valid for classes is the dot operator (.). A reference variable uses the dot operator to access public members; classes can use the dot operator to access public static members.


Assignment Operator and Classes: A Precaution

In shallow copying, two or more reference variables of the same type point to the same object.  In deep copying, each reference variable refers to its own object.  A way to avoid shallow copying of data is to have the object being copied create a copy of itself, and then return a reference to the copy.

Teaching Tip

Shallow copying is a common mistake, especially by beginning Java programmers.


Class Scope

A reference variable has the same scope as other variables. A member of a class is local to the class. You access a public class member outside the class through the reference variable name or the class name (for static members) and the member access operator (.).


Methods and Classes

Reference variables can be passed as parameters to methods and returned as method values.


Definitions of the Constructors and Methods of the class Clock

All material covered above is further explained through a detailed example of defining the class Clock.

In general, when you write the definition of a method of a class and the method uses an object of that class, then within the definition of the method the object can access its private data members (in fact, any private member of the class.)


Definition of the class Clock
A precondition is a statement specifying the condition(s) that must be true before the function is called.

A postcondition is a statement specifying what is true after the function call is completed.

Once a class is properly defined and implemented, it can be used in a program. A program or software that uses and manipulates the objects of a class is called a client of that class.

Teaching Tip

In a class definition, it is a common practice to list all the instance variables, named constants, other data members, or variable declarations first, then the constructors, and then the methods.


Copy Constructor

The copy constructor is a special constructor that executes when an object is instantiated and initialized using an existing object.  It is very useful and will be included in most of the classes. 

The syntax of the heading of the copy constructor is:

public ClassName(ClassName otherObject)


Classes and the Method toString

Whenever a class is created, Java provides the method toString to the class. This method is used to convert an object to a String object. The methods print and println output the string created by the method toString. 

The default definition of the method toString creates a string that is the name of the object’s class, followed by the hash code of the object. 

The method toString is a public value-returning method. It does not take any parameters and returns the address of a String object.

The heading of the method toString is:

public String toString()

You can override the default definition of the method toString to convert an object to a desired string.


Static Members of a Class

The modifier static in the heading of a method specifies that the method can be invoked by using the name of the class. Similarly, if a data member of a class is declared using the modifier static, it can be accessed by using the name of the class. 

In essence, public static members of a class can be accessed either by an object, that is, by using a reference variable of the class type, or using the class name and the dot operator.


static Variables (Data Members) of a Class

For each static data member of the class, Java allocates only one memory space. Static data members of a class exist even when no object of the class type is instantiated. Moreover, static variables are initialized to their default values.

Teaching Tip

A static method cannot use anything that depends on a calling object.


Finalizers

Finalizers are special types of void methods.  A class can have only one finalizer, and the finalizer can have no parameters. The name of the finalizer is finalize. The method finalize automatically executes when the class object goes out of scope.


Accessor and Mutator Methods

Every class has methods that only access and do not modify the data members, called accessor methods, and methods that modify the data members, called mutator methods.

Typically, mutator methods begin with the word set and accessor methods begin with the word get.

A well-designed class uses private instance variables and mutator and accessor methods to implement the OOD principle of encapsulation.


Creating Your Own Packages

As you develop classes, you can create packages and categorize your classes. You can import your classes in the same way that you import classes from the packages provided by Java.  Every Java SDK usually contains a directory where the compiled versions of the classes are stored.

To create a package and add a class to the package so that the class can be used in a program,
you do the following:

1.      Define the class to be public. If the class is not public, it can be used only within the package.

2.      Choose a name for the package. To organize your package, you can create subdirectories within the directory that contains the compiled code of the classes.

The next step is to compile the file using the compile command in the SDK that you are using. If you are using JDK 5.0, which contains a command-line compiler, you include the option –d to place the compiled code of the program in a specific directory.

Once the package is created, you can use the appropriate import command and reserved word package in your program to make use of the class.

If you are using an SDK to create Java programs, then you need to be familiar with the commands to compile and execute them. Typically, an SDK automatically stores the compiled code of the classes in an appropriate subdirectory.

Teaching Tip

When you install JDK 5.0 in the Windows (XP) environment, the system creates two main subdirectories: Java\jdk1.5.0 and Java\jre1.5.0. These two subdirectories are typically created within the directory c:\Program Files. The files necessary to compile and execute Java programs are placed in these subdirectories, along with other files.

Teaching Tip

In the Windows (XP) environment, you can set the environment variable CLASSPATH so that when you execute a Java program, the system can find the compiled code of the program. Check your operating system’s documentation to learn how to set the environment variables.

Teaching Tip

The CLASSPATH is an environment variable.  In Windows, environment variables can be set through the System Properties dialog, which can be found on the Control Panel.  In the System Properties dialog, select the advanced tab, and then the environment variables button.

Teaching Tip

You can add directories to the CLASSPATH as needed.

Teaching Tip

You should demonstrate setting or altering the CLASSPATH to students.


Multiple File Programs

If a class is to be used in only one program, or if you have divided your program so that it uses more than one class, rather than create a package, you can directly add the file(s) containing the class(es) to the program.

Most SDKs manage multiple-file programs in the form of a project.  A project consists of several files, called the project files. These SDKs include a command that allows you to add several files to a project. Also, these SDKs usually have commands such as build, rebuild, or make to automatically compile all the files required.


The Reference this

Every object has access to a reference to itself. The name of this reference is the reserved word this. Java implicitly uses the reference this to refer to both the instance variables and the methods of a class.  It also uses this reference to implement cascaded method calls.


Cascaded Method Calls

The reference this can be used to implement cascaded method calls. In a cascaded method call, the result returned by one method (a reference to an object) is used to immediately call another method.


Inner Classes

Classes that are defined within other classes are called inner classes. An inner class can be either a complete class definition, or an anonymous inner class definition (classes with no name). One of the main uses of inner classes is to handle events.




Abstract Data Types

An ADT is an abstraction of a commonly appearing data structure, along with a set of defined operations on the data structure.  Historically, the concept of ADT in computer programming developed as a way of abstracting the common data structure and the associated operations. Along the way, ADT provided information hiding. That is, ADT hides the implementation details of the operations and the data from the users of the ADT. Users can use the operations of an ADT without knowing how the operation is implemented.


Programming Example: Candy Machine

The purpose of this program is to write a Java application program that will put an existing candy machine into operation.  The candy machine sells candies, chips, gum and cookies. 

The input to this program is the item selection and the cost of the item. 
The output is the selected item.

The solution to this program consists of defining the class CandyMachine with the appropriate instance variables, creating the GUI and the appropriate methods to sell the items.



Key Terms
                                                                                                      
Ø      Abstract data type (ADT): A data type that specifies the logical properties without the implementation details.
Ø      Accessor Method: A method of a class that only accesses (that is, does not modify) the value(s) of the data member(s).
Ø      Class: Collection of a fixed number of components.
Ø      Constructor: Has the same name as the class, and executes automatically when an object of that class is created.
Ø      Copy constructor: Executes when an object is instantiated and initialized using an existing object.
Ø      Deep copy: Each reference variable refers to its own object not the same object.
Ø      Default constructor: Constructor without parameters.
Ø      Finalizers:  Methods that automatically execute when the class object goes out of scope.
Ø      Inner classes: Classes that are defined within other classes.
Ø      Instance methods: The member methods of a class.
Ø      Instance variables: The non-static data members of a class.
Ø      Member access operator: The dot . (period) operator; access members of a class.
Ø      Members: Components of a class.
Ø      Mutator Method: A method of a class that modifies the value(s) of the data member(s).
Ø      Postcondition: A statement specifying what is true after the function call is completed.
Ø      Precondition: A statement specifying the condition(s) that must be true before the function is called.
Ø      Shallow copy: two or more reference variables of the same type point to the same object.
+7 t �� ��� -align:justify;mso-layout-grid-align:none; text-autospace:none'>To overload a method name, within a class, any two definitions of the method must have different formal parameter lists.

The signature of a method consists of the method name and its formal parameter list. Two methods have different signatures if they have either different names or different formal parameter lists.

If a method is overloaded, then in a call to that method the signature, that is, the formal parameter list of the method, determines which method to execute.

Teaching Tip

Detailed formal descriptions of Java’s scoping and overloading rules can be found in the Java Language Specification:


Programming Example: Data Comparison

The input to this program is two different data files each with a course number followed by a series of scores. 

The output is a file containing the course numbers, group numbers, and the course average for each group in each course. 

The solution to this program consists of reading data from two different files and printing the results to an output file.  It also contains the user-defined methods calculateAverage and printResult.




Do you want to know the secrets of Java Programming and use it for the advancement of your career in IT? Do you have problems in your Java course? Here is an instant solution for your problem. Avail the Java training package of Prof Erwin Globio and get free tips about Java Programming Secrets. For more free information about Java and Java Training visit the following sites:http://erwinglobio.sulit.com.ph/ http://erwinglobio.multiply.com/http://erwinglobio.wordpress.com/. You may contact the Java Trainer at 09393741359 or 09323956678. Call now and be one of the Java Experts.


User Defined Methods

Do you want to know the secrets of Java Programming and use it for the advancement of your career in IT? Do you have problems in your Java course? Here is an instant solution for your problem. Avail the Java training package of Prof Erwin Globio and get free tips about Java Programming Secrets. For more free information about Java and Java Training visit the following sites:http://erwinglobio.sulit.com.ph/ http://erwinglobio.multiply.com/http://erwinglobio.wordpress.com/. You may contact the Java Trainer at 09393741359 or 09323956678. Call now and be one of the Java Experts.



Introduction

There are both predefined methods, methods that are already written and provided by Java, and user-defined methods, methods that you create.



Using methods has several advantages:

  • While working on one method, you can focus on just that part of the program and construct it, debug it, and perfect it.

  • Different people can work on different methods simultaneously.

  • If a method is needed in more than one place in a program, or in different programs, you can write it once and use it many times.

  • Using methods greatly enhances the program’s readability because it reduces the complexity of the method main.

Methods are often called modules. They are like miniature programs; you can put them together to form a larger program.


Predefined Methods

In Java, predefined methods are organized as a collection of classes, called class libraries.  The class Math contained in the package java.lang contains mathematical methods.  The method type is the data type of the value returned by the method.  Some predefined methods in the class Math are:

            Math.log10(x)
Math.abs(x)
Math.ceil(x)
Math.exp(x)
Math.floor(x)
Math.log(x)
Math.max(x, y)
Math.min(x, y)
Math.pow(x, y)
Math.round(x)
Math.sqrt(x)
Math.cos(x)
Math.sin(x)
Math.tan(x)

Teaching Tip

The method log10 is not available in JS2E versions lower than 5.0.



The class Character, also in the package java.lang, contains the following methods:

Character.isDigit(ch)
Character.isLetter(ch)
Character.isLowerCase(ch)
Character.isUpperCase(ch)
Character.isSpaceChar(ch)
Character.isWhitespace(ch)
Character.toLowerCase(ch)
Character.toUpperCase(ch)

Teaching Tip

The complete J2SE 1.5.0 API specification, which includes documentation for all predefined classes and methods in the J2SE 1.5.0 release, can be found at:


Using Predefined Methods in a Program

In general, to use predefined methods of a class in a program, you must import the class from the package containing the class.  By default Java automatically imports classes from the package java.lang. Therefore, if you use a method from the class Math or Character you do not need to explicitly import these classes in your program.

A method of a class may contain the reserved word static (in its heading). For example, the method main contains the reserved word static in its heading. If (the heading of) a method contains the reserved word static, it is called a static method; otherwise, it is called a nonstatic method.

The heading of a method may contain the reserved word public. If the heading of a method contains the reserved word public, it is called a public method. An important property of a public and static method is that (in a program) it can be used (called) using the name of the class, the dot operator, the method name, and the appropriate parameters.

To simplify the use of (public) static methods of a class, JS2E 1.5.0 introduces the following import statements:

import static packageName.ClassName.*;
import static packageName.ClassName.methodName;

These are called static import statements.

Teaching Tip

More information on static import statements can be found at:

Teaching Tip

The static import statement is not available in versions of Java lower than 5.0, such as 1.4.0. Therefore, if you are using, say Java 1.4.0, then you must use a static method of the class Math using the name of the class and the dot operator.


User-Defined Methods

User-defined methods in Java are classified into two categories:

  • Value-returning methods—methods that have a return type.

  • Void methods—methods that do not have a return type.


Value-Returning Methods

To use value-returning methods in your programs, you must know:

  1. The name of the method.

  1. The number of parameters, if any.

  1. The data type of each parameter.

  1. The data type of the value computed (that is, the value returned) by the method, called the type of the method.

  1. The code required to accomplish the task. (for a user-defined method)

The first four properties become part of what is called the heading of the method; the fifth property is called the body of the method. Together, these five properties form what is called the definition of the method.



Because the value returned by a value-returning method is unique, it is natural for you to use the value in one of three ways:

  • Save the value for further calculation.

  • Use the value in some calculation.

  • Print the value.

A value-returning method is used in an expression.

A Formal parameter is a variable declared in the method heading.  An Actual parameter is a variable or expression listed in a call to a method.

Teaching Tip

For predefined methods, you need to be concerned only with the first four properties. Software companies do not give out the actual source code, which is the body of the method.


Syntax: Value-Returning Method

The syntax of a value-returning method is:

modifier(s) returnType methodName(formal parameter list)
{
statements
}

  • The modifier(s) indicates the visibility of the method, that is, where in a program the method can be used (called). Some of the modifiers are public, private, protected, static, abstract, and final.

  • returnType is the type of value that the method returns. This type is also called the type of the value-returning method.

  • methodName is a Java identifier, giving a name to the method.

  • Statements enclosed between curly braces form the body of the method.

Teaching Tip

Abstract methods are covered in Chapter 11. Chapter 8 describes, in detail, the meaning of the modifiers public, private, and static.


Syntax: Formal Parameter List

The syntax of a formal parameter list is:

dataType identifier, dataType identifier,...


Method Call

The syntax to call a value-returning method is:

methodName(actual parameter list)


Syntax: Actual Parameter List

The syntax of an actual parameter list is:

expression or variable, expression or variable, ...

A method’s formal parameter list can be empty, but the parentheses are still needed. If the formal parameter list is empty in a method call, the actual parameter list is also empty.

In a method call, the number of actual parameters, together with their data types, must match the formal parameters in the order given.

Teaching Tip

A static method cannot call a nonstatic method of the class.


return Statement

A value-returning method uses a return(s) statement to return its value;


Syntax: return Statement

The return statement has the following syntax:

return expr;

where expr is a variable, constant value, or expression. The expr is evaluated and its value is returned. The data type of the value that expr computes should be compatible with the return type of the method.

When a return statement executes in a method, the method immediately terminates and control goes back to the caller.


Final Program

You can put methods within a class in any order.

A value-returning method must return a value.


Programming Example: Palindrome Number

An integer is a palindrome if it reads backwards and forwards the same way.

The input to this program is an integer (positive or negative).  The output to this program is a message indicating whether the integer is a palindrome.

The solution to this program consists of writing a method isPalindrome.  The method returns a Boolean value.  Its only parameter is the integer string that needs to be checked.  The body of the method gets the length of the string and using a for loop compares the first half of the string to the last half one character at a time.  If the first and last character don’t match the method returns false. Otherwise, it increments the position of the first character being checked and decrements the position of the last character checked and compares them.  This is done until the characters no longer match.  If they match till the end of the loop the method returns true.


Flow of Execution

When the program executes, the execution always begins with the first statement in the method main.  User-defined methods execute only when they are called.  A call to a method transfers control from the caller to the called method.  In a method call statement, you specify only the actual parameters, not their data type or the method type.  When a method exits, the control goes back to the caller.


Programming Example: Largest Number

The input to this program is a set of 10 numbers and the output is the largest of the numbers. The program can be easily modified to accommodate any set of numbers. 



The solution to this program consists of writing a method that compares two numbers and returns the larger of the two.  This method is then called in a for loop in the main method of the program.  The first number entered is stored as the maximum, and as each new number is entered it is compared to the current maximum.  The maximum is then replaced if the new number is larger than the current maximum.


Void Methods

Void methods do not have a data type.  Because a void method does not have a data type, returnType in the heading part and the return statement in the body of the void method are meaningless. However, in a void method, you can use the return statement without any value; it is typically used to exit the method early.

Because void methods do not have a data type, they are not used in an expression. A call to a void method is a stand-alone statement.


Void Methods without Parameters


Method Definition

The general form (syntax) of a void method without parameters is as follows:

modifier(s) void methodName()
{
statements
}


Method Call (Within the Class)

A method call has the following syntax:

methodName();

Because these methods do not have parameters, within a class such methods are usually good for displaying information about the program or for printing statements.


Void Methods with Parameters

Parameters provide a communication link between the calling method and the called method. They enable methods to manipulate different data each time they are called.


Method Definition

The definition of a void method with parameters has the following syntax:

modifier(s) void methodName(formal parameter list)
{
statements
}


Formal Parameter List

A formal parameter list has the following syntax:

dataType variable, dataType variable, ...


Method Call

A method call has the following syntax:

methodName(actual parameter list);


Actual Parameter List

An actual parameter list has the following syntax:

expression or variable, expression or variable, ...

As with value-returning methods, in a method call the number of actual parameters, together with their data types, must match the formal parameters in the order given.


Primitive Data Type Variables as Parameters

When a method is called, the value of the actual parameter is copied into the corresponding formal parameter. If a formal parameter is a variable of a primitive data type, then after copying the value of the actual parameter, there is no connection between the formal parameter and the actual parameter; a formal parameter of the primitive type cannot pass any result back to the calling method. When the method executes, any changes made to the formal parameters do not in any way affect the actual parameters. The actual parameter has no knowledge of what is happening to the formal parameter. Thus, formal parameters of the primitive data types cannot pass information outside the method. Formal parameters of the primitive data types provide only a one-way link between actual parameters and formal parameters.


Reference Variables as Parameters

A reference variable contains the address (memory location) of the actual data; both the formal and the value parameters refer to the same object. Therefore, reference variables can pass one or more values from a method and can change the value of the actual parameter.

Reference variables, as parameters are useful in three situations:

  1. When you want to return more than one value from a method

  1. When the value of the actual object needs to be changed

  1. When passing the address would save memory space and time, relative to copying a large amount of data


Parameters and Memory Allocation

When a method is called, memory for its formal parameters and variables declared in the body of the method, called local variables, is allocated in the method data area. The value of the actual parameter is copied into the memory cell of its corresponding formal parameter. If the parameter is an object (of a class type), both the actual parameter and the formal parameter refer to the same memory space.




Reference Variables of the String Type as Parameters: A Precaution

In the case of reference variables of the type String, we can use the assignment operator to allocate memory space to store a string and assign the string to a String variable.  If you pass a String variable (a reference variable of the String type) as a parameter to a method, and within the method you use the assignment operator to change the string, the string assigned to the actual parameter does not change; a new string is assigned to the formal parameter.

Teaching Tip

Illustrate the above concept with the example and figures on pages 396-400 in the test, or a similar example with illustrations.


class StringBuffer

If you want to pass strings as parameters to a method and want to change the actual parameter, you can use the class StringBuffer, where strings assigned to StringBuffer variables can be altered using various methods provided to manipulate strings (e.g. append, delete).


Scope of an Identifier Within a Class

The scope of an identifier refers to what other parts of the program can “see” an identifier; where it is accessible (visible).

A local identifier is an identifier declared within a method or block that can be accessed only by code within that same method or block.  Java does not allow the nesting of methods. That is, you cannot include the definition of one method in the body of another method.

The following should be noted:

  • Java does not allow the nesting of methods.

  • Within a method or a block, an identifier must be declared before it can be used. Note that a block is a set of statements enclosed within braces. A method’s definition can contain several blocks. The body of a loop or an if statement also forms a block.


  • Within a class, outside every method definition (and every block), an identifier can be declared anywhere.

  • Within a method, an identifier used to name a variable in the outer block of the method cannot be used to name any other variable in an inner block of the method.

The following are scope rules of an identifier declared within a class and accessed within a method (block) of the class.

  • An identifier, say x, declared within a method (block) is accessible:

    • Only within the block from the point at which it is declared until the end of the block.

    • By those blocks that are nested within that block.

  • Suppose x is an identifier declared within a class and outside every method’s definition (block).

    • If x is declared without the reserved word static (such as a named constant or a method name), then it cannot be accessed within a static method.

    • If x is declared with the reserved word static (such as a named constant or a method name), then it can be accessed within a method (block), provided the method (block) does not have any other identifier named x.


Method Overloading: An Introduction

Method overloading is creating several methods, within a class, with the same name. 

Two methods are said to have different formal parameter lists if both methods have:

  • A different number of formal parameters, or

  • If the number of formal parameters is the same, then the data type of the formal parameters, in the order you list, must differ in at least one position.

To overload a method name, within a class, any two definitions of the method must have different formal parameter lists.

The signature of a method consists of the method name and its formal parameter list. Two methods have different signatures if they have either different names or different formal parameter lists.

If a method is overloaded, then in a call to that method the signature, that is, the formal parameter list of the method, determines which method to execute.

Teaching Tip

Detailed formal descriptions of Java’s scoping and overloading rules can be found in the Java Language Specification:


Key Terms

Ø      Actual parameter: a variable or expression listed in a call to a method.
Ø      Class libraries: organized collection of classes.
Ø      Formal parameter: a variable declared in the method heading.
Ø      Local identifier: an identifier declared within a method or block that can be accessed only by code within that same method or block.
Ø      Local variables: variables declared in the body of the method.
Ø      Method type: data type of the value returned by the method.
Ø      Modules: another name for methods.
Ø      Palindrome: integer or string that reads the same forwards and backwards.
Ø      Parameters: arguments of a method.
Ø      Predefined methods: methods that are already written and provided by Java.
Ø      Scope: refers to what other parts of the program can “see” an identifier.
Ø      User-defined methods: methods that the programmer creates.
Ø      Value-returning methods: methods that have a return type.


Void methods: methods that do not have a return type.





Do you want to know the secrets of Java Programming and use it for the advancement of your career in IT? Do you have problems in your Java course? Here is an instant solution for your problem. Avail the Java training package of Prof Erwin Globio and get free tips about Java Programming Secrets. For more free information about Java and Java Training visit the following sites:http://erwinglobio.sulit.com.ph/ http://erwinglobio.multiply.com/http://erwinglobio.wordpress.com/. You may contact the Java Trainer at 09393741359 or 09323956678. Call now and be one of the Java Experts.