User Defined Methods inward Java
i) Introduction to Java Methods
ii) Types of Methods
iii) User defined Methods
-----------------------------------
i) Introduction to Java Methods
1) What is Method?
> H5N1 Java method is a fix of statements that are grouped together to perform an operation.
> Methods are besides known equally Functions
> In Structured programming (ex: C Language) nosotros role Functions (Built inward as well as User defined)
> In Object Oriented Programming nosotros role Methods (Built inward as well as User defined)
2) When nosotros guide Methods?
Whenever nosotros desire to perform whatever functioning multiple times thus guide methods.
3) Advantages of Methods
Code Reusability as well as Reduce the projection code size.
---------------------------------
ii) Types of Methods
Basically nosotros convey two types of Methods
1) Built inward Methods
2) User defined Methods
-----------------------------
1) Built inward Methods
Java has a library of classes as well as methods organized inward packages.
Ex:
import java.io.Console;
import java.io.*;
> In social club to role built inward methods nosotros ask to import packages or classes.
> java.lang packet is automatically imported inward every Java program.
> Using import keyword nosotros tin import packages/classes.
-----------------------------------------
Categories of Built inward Methods.
1) String Methods
2) Array Methods
3) Number Methods
4) Character Methods
Etc....
---------------------------------
iii) User defined Methods
Two types of User defined Methods
1) Method without returning whatever value
a) Calling Method past times invoking Object
b) Calling Method without invoking Object
2) Method alongside returning a value.
-------------------------------
a) Calling Method past times invoking Object
b) Calling Method without invoking Object
---------------------------------------------------
1) Writing Methods (With returning a value)
a) Syntax for creating a method (calling the method past times invoking object)
Write Method
//Before principal Method
accessModifier returnType methodName(Parameters){
Statements
---------
---------
------
}
----------------------
//After principal method
Call Method
//Create Object
ClassName objectName = novel ClassName();
//Call Method y invoking object
dataType variableName = object.method(values..);
Or
System.out.println(object.method());
-----------------------------------
Example:
package xyza;
public class JavaMethods {
//User defined Method
public int multiply(int a, int b, int c){
int effect = a * b * c;
return result;
}
public static void principal (String [] args){
//Create Object
JavaMethods abc = novel JavaMethods();
//Call Method
int x = abc.multiply(10, 25, 35);
System.out.println(x);
System.out.println(abc.multiply(10, 25, 35));
}
}
------------------------------------------------
1) Writing Methods (With returning a value)
b) Syntax for creating a method (calling the method without object)
accessModifier nonAccessModifier returnType methodName(Parameters){
Statements
------
------
------
}
Call Method
dataType variableName = methodName(values);
Or
System.out.println(methodname(values);
------------------------------------------
Example:
package xyza;
public class JavaMethods {
//Create Method
public static int multiply(int a, int b, int c){
int effect = a * b * c;
return result;
}
public static void principal (String [] args){
//Call Method
int x = multiply(10, 25, 35);
System.out.println(x);
System.out.println(multiply(10, 25, 35));
}
}
-----------------------------------------
2) Write method without returning whatever value
a) Syntax for creating a Method (call the method past times invoking Object)
accessModifier returnTypeNothing methodName(parameters){
Statements
----------
----------
---------
}
//Create Object
ClassName objectName = novel ClassName();
//Call Method
object.method(values);
-------------------------------------
Example:
public class JavaMethods {
//Create Method
public void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A");
}
else if (marks >= 500){
System.out.println("Rank B");
}
else
System.out.println("Rank C");
}
public static void principal (String [] args){
//Call method past times invoking object
JavaMethods obj = novel JavaMethods();
obj.studentRank(600);
}
}
------------------------------
2) Write method without returning whatever value
b) Syntax for creating a Method (call the method without Object)
accessModifier nonAccessModifier returnTypeNothing methoName(Parameters){
Statements
---------
---------
---------
}
Call method
methodName(values);
----------------------------------
public class JavaMethods {
//Create Method without returning whatever value (without object)
public static void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A");
}
else if (marks >= 500){
System.out.println("Rank B");
}
else{
System.out.println("Rank C");
}
}
public static void principal (String [] args){
//Call method without object
studentRank(450);
}
}
------------------------------------------
Usage of methods
a) Internal methods (defining/creating as well as calling methods inside the same class)
b) External methods (Calling methods from other classes)
------------------------------------
3a) Call External method (from only about other class) -by invoking Object
Class 1:
public class Sample1 {
//Create Method
public static int multiply(int a, int b, int c){
int effect = a * b * c;
return result;
}
}
Class 2:
public class Sample2 {
//Create method
public int add(int a, int b, int c){
int effect = a + b + c;
return result;
}
public static void principal (String [] args){
//Create Object
Sample2 obj1 = novel Sample2();
//Calling Internal method
int x = obj1.add(10, 25, 35);
System.out.println(x);
//Create Object
Sample1 obj2 = novel Sample1();
//Calling External Method
int y = obj2.multiply(10, 25, 35);
System.out.println(y);
}
}
-----------------------------
3b) Call External method (from only about other class) -without Object
Class 1:
public class Sample1 {
//Create Method
public static void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A");
}
else if (marks >= 500){
System.out.println("Rank B");
}
else{
System.out.println("Rank C");
}
}
public static void principal (String [] args){
//Call method without object
studentRank(450);
}
}
Class 2:
public class Sample2 {
public static void principal (String [] args){
Sample1.studentRank(450);
}
}
-----------------------------------
Other Topics:
1) Method OverLoading
2) Method OverRiding
-------------------------------------
i) Introduction to Java Methods
ii) Types of Methods
iii) User defined Methods
-----------------------------------
i) Introduction to Java Methods
1) What is Method?
> H5N1 Java method is a fix of statements that are grouped together to perform an operation.
> Methods are besides known equally Functions
> In Structured programming (ex: C Language) nosotros role Functions (Built inward as well as User defined)
> In Object Oriented Programming nosotros role Methods (Built inward as well as User defined)
2) When nosotros guide Methods?
Whenever nosotros desire to perform whatever functioning multiple times thus guide methods.
3) Advantages of Methods
Code Reusability as well as Reduce the projection code size.
---------------------------------
ii) Types of Methods
Basically nosotros convey two types of Methods
1) Built inward Methods
2) User defined Methods
-----------------------------
1) Built inward Methods
Java has a library of classes as well as methods organized inward packages.
Ex:
import java.io.Console;
import java.io.*;
> In social club to role built inward methods nosotros ask to import packages or classes.
> java.lang packet is automatically imported inward every Java program.
> Using import keyword nosotros tin import packages/classes.
-----------------------------------------
Categories of Built inward Methods.
1) String Methods
2) Array Methods
3) Number Methods
4) Character Methods
Etc....
---------------------------------
iii) User defined Methods
Two types of User defined Methods
1) Method without returning whatever value
a) Calling Method past times invoking Object
b) Calling Method without invoking Object
2) Method alongside returning a value.
-------------------------------
a) Calling Method past times invoking Object
b) Calling Method without invoking Object
---------------------------------------------------
1) Writing Methods (With returning a value)
a) Syntax for creating a method (calling the method past times invoking object)
Write Method
//Before principal Method
accessModifier returnType methodName(Parameters){
Statements
---------
---------
------
}
----------------------
//After principal method
Call Method
//Create Object
ClassName objectName = novel ClassName();
//Call Method y invoking object
dataType variableName = object.method(values..);
Or
System.out.println(object.method());
-----------------------------------
Example:
package xyza;
public class JavaMethods {
//User defined Method
public int multiply(int a, int b, int c){
int effect = a * b * c;
return result;
}
public static void principal (String [] args){
//Create Object
JavaMethods abc = novel JavaMethods();
//Call Method
int x = abc.multiply(10, 25, 35);
System.out.println(x);
System.out.println(abc.multiply(10, 25, 35));
}
}
------------------------------------------------
1) Writing Methods (With returning a value)
b) Syntax for creating a method (calling the method without object)
accessModifier nonAccessModifier returnType methodName(Parameters){
Statements
------
------
------
}
Call Method
dataType variableName = methodName(values);
Or
System.out.println(methodname(values);
------------------------------------------
Example:
package xyza;
public class JavaMethods {
//Create Method
public static int multiply(int a, int b, int c){
int effect = a * b * c;
return result;
}
public static void principal (String [] args){
//Call Method
int x = multiply(10, 25, 35);
System.out.println(x);
System.out.println(multiply(10, 25, 35));
}
}
-----------------------------------------
2) Write method without returning whatever value
a) Syntax for creating a Method (call the method past times invoking Object)
accessModifier returnTypeNothing methodName(parameters){
Statements
----------
----------
---------
}
//Create Object
ClassName objectName = novel ClassName();
//Call Method
object.method(values);
-------------------------------------
Example:
public class JavaMethods {
//Create Method
public void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A");
}
else if (marks >= 500){
System.out.println("Rank B");
}
else
System.out.println("Rank C");
}
public static void principal (String [] args){
//Call method past times invoking object
JavaMethods obj = novel JavaMethods();
obj.studentRank(600);
}
}
------------------------------
2) Write method without returning whatever value
b) Syntax for creating a Method (call the method without Object)
accessModifier nonAccessModifier returnTypeNothing methoName(Parameters){
Statements
---------
---------
---------
}
Call method
methodName(values);
----------------------------------
public class JavaMethods {
//Create Method without returning whatever value (without object)
public static void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A");
}
else if (marks >= 500){
System.out.println("Rank B");
}
else{
System.out.println("Rank C");
}
}
public static void principal (String [] args){
//Call method without object
studentRank(450);
}
}
------------------------------------------
Usage of methods
a) Internal methods (defining/creating as well as calling methods inside the same class)
b) External methods (Calling methods from other classes)
------------------------------------
3a) Call External method (from only about other class) -by invoking Object
Class 1:
public class Sample1 {
//Create Method
public static int multiply(int a, int b, int c){
int effect = a * b * c;
return result;
}
}
Class 2:
public class Sample2 {
//Create method
public int add(int a, int b, int c){
int effect = a + b + c;
return result;
}
public static void principal (String [] args){
//Create Object
Sample2 obj1 = novel Sample2();
//Calling Internal method
int x = obj1.add(10, 25, 35);
System.out.println(x);
//Create Object
Sample1 obj2 = novel Sample1();
//Calling External Method
int y = obj2.multiply(10, 25, 35);
System.out.println(y);
}
}
-----------------------------
3b) Call External method (from only about other class) -without Object
Class 1:
public class Sample1 {
//Create Method
public static void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A");
}
else if (marks >= 500){
System.out.println("Rank B");
}
else{
System.out.println("Rank C");
}
}
public static void principal (String [] args){
//Call method without object
studentRank(450);
}
}
Class 2:
public class Sample2 {
public static void principal (String [] args){
Sample1.studentRank(450);
}
}
-----------------------------------
Other Topics:
1) Method OverLoading
2) Method OverRiding
-------------------------------------