Developing User Defined Methods

 

Introduction.  We have now written several different programs making use of Java's built in packages, classes and methods.  The next step is to develop our own user defined methods.  This webpage will focus on the specifics of developing appropriate syntax for declaring and defining these methods.  Subsequent pages will present more specific examples.

I expect that you will go over this page and then refer back to it as we work examples.  It will make little sense the first time through.  But it should make more sense every time you encounter another method.

Why develop your own methods?

  • code can be written and reused from different locations in a program
  • methods can be more easily imported from other programs without rewriting
  • smaller segments of code are easier to debug and validate and once they are debugged and validated you know they aren't the source of problems.

Methods can be declared using the following general syntax.

modifiers returnType methodName(parameter list)

{

statements

}

While this probably makes less sense to you after having seen it we will discuss each of these.  We will start with those that are most familiar.

Return Types.  In general methods can be considered to be of two main returnTypes

  • value returning methods
    • methods that return a value of a particular type
      • can be a primitive data type such as
        • int
        • float
        • boolean
      • can be an object from a Java predefined class such as
        • String
        • Date
      • can be an object in a user defined class - which we will not do until much later in the semester
  • void methods
    • methods that return nothing

Parameter List.  A parameter list is a list of variables and/or objects that come from outside the method to be used within the method.  The developer needs to specify both the object and/or data type and give it a name or identifier.  Thus a parameter list looks like the following.

dataType identifier1, dataType identifier2, objectType identifier3 ...

where they can be listed in any order desired as long as each identifier is paired with its appropriate type.

Method Names.  Methods can be named just like any variables.  It is relatively standard practice to start with a lower case letter just like for variables.

  • start with a letter, underscore or dollar sign

    • start with lower case

    • each subsequent word starts with a capital letter

  • after this it can contain any combination of letters and/or numbers

    • no spaces

Modifiers.  Without saying very much about their meaning until we use them, some typical modifiers are

  • public
  • private
  • protected
  • static
  • abstract
  • final

Some Examples.  Now we present some examples.  We've already seen several of these.

  • init( )
  • public static void main( String args[ ] )
  • public double power( double x, double y )
    • finds the power xy
    • passes in double data types for x and y
    • returns solution as double
  • private getSolution( int numberServers, float arrivalRate, float serviceRate )
    • inputs are numberServers, arrivalRate and serviceRate
  • public long computeFactorial( long n )
    • input is the number for which you want to find the factorial
    • returns a solution with data type long
  • public Rational computeSum( Rational a, Rational b )
    • based on a user developed class called Rational
    • passes in two different fractions
    • returns the fraction solution as an object of class Rational
  •  

 

Method Invocation.  The way to cause a method to execute depends on whether it returns a value. 

  • doesn't return a value

    methodName(parameter list);

  • returns a value of type genericType
    • first declare a variable or object called generis of genericType

      generis = methodName(parameter list);

    • this allows the result that is returned to be placed into the memory location determined by generis
    • remember these data and/or object types must agree

Returning a Result.  When you want to return something back to where it was called you need to have declared an object or variable of the appropriate type earlier in the program.

genericType  generis;

Or it might be an expression that evaluates to the appropriate genericType.

Then within the method you need a statement at the  appropriate location when you want to be done with the method and return the value.

return  generis;

or

return  expression;    //  evaluates to appropriate genericType

Now we will start working examples in subsequent webpages.