Friday , December 13 2019

JAVA #18 – Funtions/Methods in Java

Defining Methods

The only required elements of a method declaration are the method’s return type, name, a pair of parentheses, (), and a body between braces, {}.

More generally, method declarations have six components, in order:

  1. Modifiers—such as public, private, and others you will learn about later.
  2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
  3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
  4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
  5. An exception list—to be discussed later.
  6. The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.

Modifiers, return types, and parameters will be discussed later in this lesson. Exceptions are discussed in a later lesson.

 

package functionjava;
public class FunctionJava { // Class
    
    public void Add() // Method/Function Decleration
    {
        System.out.println(10+20);
    }
    public void Sub()
    {
        System.out.println(10-20);
    }
    public void Mul()
    {
        System.out.println(10*20);
    }
    public void Div()
    {
        System.out.println(10/20);
    }
    
    public static void main(String[] args) {
        FunctionJava objFun = new FunctionJava(); // Object Of A Class
        objFun.Add(); // Function Calling
        objFun.Sub();
        objFun.Mul();
        objFun.Div();
        
    }
    
}

 

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

Leave a Reply

Your email address will not be published.