Java Methods - Java

What is Java Methods?

A Java technique is a group of statements which might be grouped together to perform an operation. When you call the system.out.println() method, as an instance, the machine genuinely executes several statements in order to display a message at the console.

Now you'll discover ways to create your very own techniques without or with go back values, invoke a technique with or without parameters, and apply method abstraction in the program design.

Creating Method

Considering the following example to explain the syntax of a method −

Syntax

Here,

  • public static − modifier
  • int − return type
  • methodName − name of the method
  • a, b − formal parameters
  • int a, int b − list of parameters

Method definition consists of a method header and a method body. The same is shown in the following syntax −

Syntax

The syntax shown above includes −

  • modifier − It defines the access type of the method and it is optional to use.
  • returnType − Method may return a value.
  • nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list.
  • Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
  • method body − The method body defines what the method does with the statements.

Example

Here is the source code of the above defined method called max(). This method takes two parameters num1 and num2 and returns the maximum between the two −

Method Calling

For using a method, it should be called. There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value).

The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when −

  • the return statement is executed.
  • it reaches the method ending closing brace.

The methods returning void is considered as call to a statement. Lets consider an example −

The method returning value can be understood by the following example –

Following is the example to demonstrate how to define a method and how to call it −

Example

This will produce the following result −

Output

The void Keyword

The void keyword allows us to create methods which do not return a value. Here, in the following example we're considering a void method methodRankPoints. This method is a void method, which does not return any value. Call to a void method must be a statement i.e. methodRankPoints(255.7);. It is a Java statement which ends with a semicolon as shown in the following example.

Example

This will produce the following result −

Output

Passing Parameters by using value

even as running under calling method, arguments is to be passed. these should be in the same order as their respective parameters in the method specification. Parameters may be passed by value or by reference.

Passing Parameters through value means calling a method with a parameter. via this, the argument value is passed to the parameter.

instance

the following application indicates an instance of passing parameter by value. The values of the arguments remains the same even after the technique invocation.

This will produce the following result −

Output

Method Overloading

While a category has two or extra strategies by the same name but different parameters, it is referred to as method overloading. it is different from overriding. In overriding, a method has the same method call, type, number of parameters, and many others.

Allows keep in mind the example mentioned earlier for locating minimum numbers of integer type. If, let’s say we need to find the minimum number of double type. Then the idea of overloading will be delivered to create or more methods with the same name but different parameters.

The following example explains the same –

Example

This will produce the following result −

Output

Overloading methods makes program readable. Here, two methods are given by the same name but with different parameters. The minimum number from integer and double types is the result.

Using Command-Line Arguments

Sometimes you will want to pass some information into a program when you run it. This is accomplished by passing command-line arguments to main( ).

A command-line argument is the information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy. They are stored as strings in the String array passed to main( ).

Example

The following program displays all of the command-line arguments that it is called with –

Try executing this program as shown here –

This will produce the following result −

Output

The Constructors

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors don't have any explicit return type.

Typically, you'll use a constructor to give initial values to the example variables defined by the class or to perform any other startup strategies required to create a fully formed object.

All classes have constructors, whether you define one or not because Java automatically provides a default constructor that initializes all member variables to zero. However, when you outline your personal constructor, the default constructor is no longer used.

example

Here’s a simple example that uses a constructor without parameters –

You will have to call constructor to initialize objects as follows –

Output

Parameterized Constructor

Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor's name.

Example

Here is a simple example that uses a constructor with a parameter –

You will need to call a constructor to initialize objects as follows –

This will produce the following result −

Output

The this keyword

this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.

Note − the keyword this is used only within instance methods or constructors

this

In general, the keyword this is used to −

Differentiate the instance variables from local variables if they have same names, within a constructor or a method.

Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation.

Example

Here is an example that uses this keyword to access the members of a class. Copy and paste the following program in a file with the name, This_Example.java.

This will produce the following result −

Output

Variable Arguments(var-args)
JDK 1.5 enables you to pass a variable number of arguments of the same type to a method. The parameter in the method is declared as follows –

In the method declaration, you specify the type followed by an ellipsis (...). Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it.

Example

This will produce the following result −

Output

The finalize( ) Method

It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.

For example, you might use finalize( ) to make sure that an open file owned by that object is closed.

To add a finalizer to a class, you simply define the finalize( ) method. The Java runtime calls that method whenever it is about to recycle an object of that class.

Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed.

The finalize( ) method has this general form –

Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class.
This means that you cannot know when or even if finalize( ) will be executed. For example, if your program ends before garbage collection occurs, finalize( ) will not execute.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Topics