Method & its types
{
Statements;
}
{
System.out.println("Hello Sarthak");
}
s1.input_data();
s1.input_data();
s1.input_data();
Types of methods:
1. no argument method or no-parameterized method
2. argument method or parameterized method
{
Statements;
}
{
System.out.println("Hello "+name);
}
How to call/executing parameterized method?
s1.input_data(name);
name2="ADPatel"
name3="SDPatel"
s1.input_data(name1);
s1.input_data(name2);
s1.input_data(name3);
- A method is a block of code that only runs when it is called.
- You can pass data, known as parameters, into a method.
- Methods are used to perform certain actions, and they are also known as functions.
- Why use methods? To reuse code: define the code once, and use it many times.
- A method must be declared within a class.
- It is defined with the name of the method, followed by parentheses ().
- Example:
- Syntax:
{
Statements;
}
- Example:
{
System.out.println("Hello Sarthak");
}
- Here,void means that this method does not have a return value.
- input_data() is the name of the method.
- () has no arguments.
- To call a method in Java, write the method's name followed by two parentheses () and a semicolon;
- Syntax:
- Example:
- A method can also be called multiple times:
s1.input_data();
s1.input_data();
s1.input_data();
Types of methods:
1. no argument method or no-parameterized method
2. argument method or parameterized method
- Information can be passed to methods as a parameter.
- Parameters act as variables inside the method.
- Parameters are specified after the method name, inside the parentheses.
- You can add as many parameters as you want, just separate them with a comma.
- Syntax:
{
Statements;
}
- Example:
{
System.out.println("Hello "+name);
}
How to call/executing parameterized method?
- To call a method in Java, write the method's name followed by two parentheses (arguments) and a semicolon;
- Syntax:
- Example:
s1.input_data(name);
- A method can also be called multiple times:
- Example:
name2="ADPatel"
name3="SDPatel"
s1.input_data(name1);
s1.input_data(name2);
s1.input_data(name3);
Tags:
Core java