Search Java Tutorials

Custom Search

What You Will Want to Complete This Tutorial?

You must have at least 1st and 2nd pre-requirements to start the tute. Other requirements will require for last chapters only. These all are free to use and download!
  1. Java Development Kit Standard Edition (JDK / J2SE) Any Version(www.java.com)
  2. Any Text Editor (Note Pad quite enough or get Notepad++ from (http://notepad-plus.sourceforge.net)
  3. Java Run Time Environment (JRE) Any Version(www.java.com)
  4. Java Enterprise Edition (J2EE) for advanced developing (For last few chapters)(www.java.com)
  5. Java Wireless Toolkit for Mobile Application Development (For Mobile Application Development Chapters) (www.java.com)
  6. Tomcat Web Server to run Servlets. (www.tomcat.apache.org/)
Your operating system may be any version of Windows.
What will you learn (As a summary)?
  1. How to build console and windowed applications using Java
  2. How to build web page elements with Java Applets
  3. How to build Database Programs with Java
  4. How to build enterprise applications with Beans
  5. How to add Email functions to Java programs
  6. How to program smart devices (Phone, PDA etc…) with Java

Improving Method Declaration

In this chapter we will discuss about some declarations we used in previous chapters.

How to implement method declaration?

A Method is a series of statements that carry out a task. A method declaration include, as we mentioned earlier:

  1. Declaration
    1. Optional access modifiers (public, private, protected …)
    2. The return type of the method (void or returning data type)
    3. The method name (Ex: main)
    4. An opening parenthesis
    5. Optional list of method arguments
    6. Closing parenthesis
  1. An Opening curly brace
  2. A body
  3. A closing curly brace

Any method can be called by other methods under certain limitations added by access modifier. “public” is no restrictions, every one can access it. “private” is to restrict method access only within the class it declared.

Ex: public void calculateMethod() {}

Ex: private void idNumberMethod() {}

SCJP Tip: you can use more-than-one modifier while declaring class or its members. When you do so, the modifiers may appear in any order in that declaration. For example , you can apply the access modifiers either before or after or in between the other modifiers.

Arguments or parameters consist of information that a method requires to perform certain task. You can provide as many as parameters within braces in line with method declaration.

Ex: public void idNumberMethod (int myNumber)

Indicating method name with arguments within parenthesis can make a call to that method. A method in a class can be called from another class.

Ex: calculateMethod(319);

Sample Program:

Case: This program will print a literal string from method named “printSomething”. ‘main’ method call this method in line 5. Check line 5 again. You may find that this calling do not send any arguments as the parenthesis is empty. Also in the method declaration in line 8, you can see empty parenthes is. Save this code as “Caller.java”.

This is the output:

When you write a method declaration for a method that can receive an argument, you must include the type of the argument and a local name for that argument.

Sample Program:

Case: This is an implementation of “Caller.java”. Instead of print something, main method send a text to printSomething method.

This is the output:

A method may require more than one argument. If so, you have to indicate data type of each argument separately and to define a local name. Arguments can be separated by commas.

Ex: public void calculateMethod(int Number1, intNumber2, String operator)

To make a call to this method: calculateMethod(22, 4, “+”);

Sample Program:

Case: You have two numbers to multiply. You can send those numbers to another method as arguments.

This is the output:

SCJP Tip: That’s basic again! Now the real declaration of a method:

[modifiers] returntype methodName([argumentList]) {

[method body]

}

[modifiers] : You know! It’s ‘public’ like thing!

returntype : If you want to return something (As in next sample program) then you want to state its data type here.

methodName : ID of your method (Method Name).

([argumentList]) : Arguments that the method will receive.

Tip: There’re a method type known as ‘Abstract Methods’. They have no method body and instead a semi colon will be at the end of method declaration. You’ll learn these later.

Ex: public string find(String wordToFind);

No comments: