Showing posts with label Method signature. Show all posts
Showing posts with label Method signature. Show all posts

Monday, February 25, 2013

Methods and method parameters

Consider a method (also called a behaviour) for logging on to a network. When a user logs on, we could reasonably assume that the user must provide a username and password. A method for logging on could look like this:


This method signature can be broken down as follows:

public - The visibility of the method. A method that is public (as opposed to private) can be seen by other classes.

void - This indicates that the method will not return a value. Some methods return values (as in the case of the Scanner’s nextInt() function).

login - This is the name of method. Method names are named in the same way as variables in that they should start with a lower case letter. If the method name contains serveral words, each subsequent word starts with an uppercase letter. Method names must be unique - except where method overloading is used.

(String userName, String password) - This is the parameter list for method. When a method requires some data to perform its task, a list of that data is specified. This parameter list contains 2 parameters (although a parameter list may contain more, less or no parameters). 

Each parameter has a name, a type and is separated by a comma.

Wednesday, September 5, 2012

Method Overloading


You may have many methods with the same name in a class. This is known as method overloading. 

The rule is that each parameter list must be different. This applies to constructors as well.

doFoo(int param1, int param2)
doFoo(String param1, int param2)
Correct: Same name, unique parameter list.

doFoo(int param1, int param2)
doFoo(int param1, int param2)
Incorrect: Same name, same parameter list.

Friday, May 18, 2012

Parameters / Arguments



See also:
"Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/XxwPUf )

Tuesday, March 27, 2012

Methods: Procedures & Functions

DOWNLOAD THE CODE: MethodsPkg

There are two types of methods:
  1. Procedure
  2. Function
All methods begin with a method signature which opens into a code block.

A method signature is a contract which specifies:

  • visibility (can it be seen externally?)
  • what type of data it will return
  • what it will do (the name)
  • which data, if any, it requires to perform it's task

visibility | return data type | method name | (parameters if any) {

}



Procedure
What it does: executes processing statements.

Example:



The method signature for a procedure always contains the keyword void (meaning nothing/empty). This is as opposed to a function, which always returns a value. Read on...


Function
What it does: executes processing statements and returns a value to the calling procedure.

Example:



The method signature for a function always contains a datatype to indicate the type of data that will be returned from the function.

The final statement in a function begins with the return keyword, followed by the data that will be returned. In this case, a String.

Methods may also require incoming values called parameters (sometimes called arguments) which are placed within the parenthesis following the method name.

Monday, March 19, 2012

The Static keyword

The static keyword is a Java modifier which associates a class member or method with a class rather than an instance of a class.

For instance, the main method of a class is always static as there must be exactly one instance of the class containing main in order for the compiler to find it.

A class containing static members or methods does not require instantiation in order to use the static methods of the class.

Sunday, March 18, 2012

The Main method

A Java project many contain many packages and each package may contain many classes. There must be at least one class containing a main method.

The main method is a static method, belonging to the class, where program execution begins - it's the front door, so to speak. The main method begins the chain of events that load the program into memory, ready for use.

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)


The main method can optionally take a String array as a parameter, as shown in the parenthesis.

(String[] args)