Methods
Methods in
Java are the same as functions in another programming language. It will execute a
block of code whenever it is being called.
Syntax
--Declaring a
method ---
Datatype
methodname(){
//block of
code
}
--calling a
method---
methodname()
Types
of Methods
1.
Method with parameter without return type.
2.
Method without parameter without return type.
3.
Method with parameter with return type.
4.
Method without parameter with return type.
Parameters
and Arguments
They are the
values that are passed during the run time of the program.
Java
Scope
In Java, variables are only accessible inside
the region they are created. This is called scope.
Note:
static
method, which means that it can be accessed without creating an object of the
class, unlike public, which can only be accessed by objects
Java
Class
•
A class is a blueprint for the object. Before
we create an object, we first need to define the class.
•
Class is a user-defined data type that defines
data and functions
Create
a class in Java
We can create a class in Java using the
class keyword.
Syntax:
class ClassName
{
// variables
// methods
}
•
Here, variables
and methods represent the state and behavior of the object
respectively.
•
variables are used to store data and adding
variables inside the body of the class are called instance variables/attribute
•
methods are used to perform some operations
and adding methods inside the body of the class is called instance methods
Objects
An object is called an instance of a
class.
Creating
an Object in Java or instantiation
Here is how we can create an object of
a class.
className object = new className();
//creating a reference variable.
E.g: circle c=new circle();
Steps
for using class and object
1.
Define a class by adding variables and
methods
2.
Creating objects
3.
Accessing the class members
4.
Example 01:program to find the area of a triangle using class and object
class
triangle //class definition
{
double base,height,area;
void getinput()
{
base=10.5;
height=5.3;
}
void calculate()
{
area=(0,5)*base*height;
}
void display()
{
System.out.println(“Area of
triangle=“+area);
}
}
class triangledemo
{
public
static void main(String args[])
{
triangle
t=new triangle(); //creating object
t.getinput(); //accessing methods
t.calculate();
t.display();
}
}
Example 02:
package Methods;
class Methods {
//Method without parameter without return type
public void add() {
int a = 123;
int b = 10;
System.out.println("Addition : " + (a + b));
}
//Method with parameter without return type
public void sub(int x, int y) {
System.out.println("Subtraction : " + (x - y));
}
//Method without parameter with return type
public int mul() {
int a = 123;
int b = 10;
return a * b;
}
//Method with parameter with return type
public float div(int x, int y) {
return (x / y);
}
//Recursion Function
public int factorial(int n)//5! =1*2*3*4*5=120
{
if(n==1)
return 1;
else
return (n*factorial(n-1));
}
/*
factorial(5)
factorial(4)
factorial(3)
factorial(2)
factorial(1)
return 1;
return 2*1;
return 3*2;
return 4*6;
return 5*24;
* */
}
//Type of User Define Methods in Java
public class functions {
public static void main(String args[]) {
Methods o = new Methods();
o.add();
o.sub(123, 10);
System.out.println("Muli : "+o.mul());
System.out.println("Division : "+o.div(123,10));
}
}
0 Comments