Java Tutorial – Method

A java method is a collection of statements that are grouped together to perform an operation.When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on teh console.

Now you will learn how to create your own methods with or without returning values, invokes a method with or without parameters, and apply method abstration ba batil in the program design.

Creating method:

Consider the following example to explain the syntax of a method:

here ,
public static: modifier
int : is 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 below:

modifier: it defines the access type of the method and it is optional to use.

returnType: method may return a value

nameOfTheMethod: This is the method name.The method signature consists of the method name and the parameter list.

Parameter List:The list of parameter 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 statements.

Example:

Method Calling:
– method returns a value
– returning nothing

example:

method is the just like function in c/c++
output:

The void keyword:
The void keyword allows us to create methods which do not return a value.here in the void method methodRankPoints().This method is a void method which does not return any value.
example:

output:

Passing Parameters by value:

output:

Method Overloading:
When a class has two or more methods by same name but different parameters, it is known as method overloading.It is different from overriding.In overriding a method has same method name,type,number of parameters etc.

example:

output:

Using Command Line Arguments:

Sometimes you need to pass command to a  program by command line arguments into main()

The Constructors:
Parametarized constructor:
Here is a simple example that uses a constructor with parameter:

You would call constructor to initialize objects as follows:

output:

The this keyword:
this is a keyword in Java which is used as a reference to the object of the current class, with 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.
Java Tutorial

In general the keyword this is used to:
Differentiate the instance variables from local variables if they have same name, 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.

This এর ব্যাপারটা আরো ক্লিয়ার করতে হবে পরে

Variable Arguments(var-args)
typeName…parameterName

there is another method finalize() method
syntax: