Introducing Method
In java programming language class usually consist of two things instance variable and methods.
What is a method in java
Method are the block of statement that perform a special task.
Using methods we can avoid the rewriting the same code over and over in a sane program.
Method increase the efficiency and reduce the complexity of the program.
Syntax of Method Definition
following is the Java method syntax:
1 2 3 | accessModifier returnType methodName(){ statements; } |
Write a program to demonstrate method in class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class RectangleArea { int length; int width; void area() { // method area() definition length = 10; // within class variables access without object width = 20; int a = length * width; System.out.println("Area of the rectangle =" + a); } } public class TestRectangle { public static void main(String arg[]) { RectangleArea obj1 = new RectangleArea(); obj1.area();// call area() method } } |
Result
1 | Area of the rectangle =200 |
Creating a method in java
to create a method we have to give method heading and method body.
As you can already seen here
1 2 3 4 5 6 | void area() { // method area() definition length = 10; // within class variables access without object width = 20; int a = length * width; System.out.println("Area of the rectangle =" + a); } |
how to call a method in java
In above program you can see how method area is called in main().
Object.methodName(parameter list);
is used to call methods from another/same class.
A static method can be called with Class name or with object.
ClassName.methodName(parameter list);
Parameterized(overloaded) Method
Write a program to demonstrate parameterized method in class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class RectangleArea { int length; int width; void area(int i, int j) { // method area() definition length = i; // within class variables access without object width = j; int a = length * width; System.out.println("Area of the rectangle =" + a); } } public class TestRectangle1 { public static void main(String arg[]) { RectangleArea obj1 = new RectangleArea(); obj1.area(10, 20);// call area() method and pass value of argyments } } |
Output
1 | Area of the rectangle =200 |
Java Program to illustrate Method call with and without agrument and with and without return type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class MethodsDemo1 { public static void main(String[] args) { System.out.println("Inside Main method"); MethodsDemo1 m = new MethodsDemo1(); m.add(); int x = 5, y = 7; m.sub(x, y); //10,5 are actual parameter int mulVal = m.mul(); System.out.println("Multiplication is " + mulVal); int divVal = m.div(10, 5); //10,5 are actual parameter System.out.println("Division is " + divVal); } //method without argument and without return type public void add() { int a = 5, b = 5; int result = a + b; System.out.println("Sum is " + result); } //method with argument and without return type public void sub(int a, int b) { //a and b are formal arguments int result = a - b; System.out.println("Subtraction is " + result); } //method without argument and with return type public int mul() { int a = 5, b = 2; int result = a * b; return result; } //method with argument and with return type public int div(int a, int b) { //a and b are formal arguments int c = a / b; return c; } } |
Output
1 2 3 4 5 6 7 8 | D:\Test Java>javac MethodsDemo1.java D:\Test Java>java MethodsDemo1 Inside Main method Sum is 10 Subtraction is -2 Multiplication is 10 Division is 2 |
Java Program to pass a object to a method
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Student { private long rollNo; private String name; private String branch; public void setRollNo(long rollNo1) { rollNo = rollNo1; } public long getRollNo() { return rollNo; } public void setName(String name1) { name = name1; } public String getName() { return name; } public void setBranch(String branch1) { branch = branch1; } public String getBranch() { return branch; } } |
StudentDemo.java contains main method and updateStudent method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class StudentDemo { public static void main(String[] args) { Student student = new Student(); student.setRollNo(1); student.setName("Ram"); student.setBranch("CSE"); System.out.println("Student Details"); System.out.println("Roll No " + student.getRollNo() + " Name " + student.getName() + " branch " + student.getBranch()); StudentDemo s = new StudentDemo(); s.updateStudent(student); System.out.println("Roll No " + student.getRollNo() + " Name " + student.getName() + " branch " + student.getBranch()); } public void updateStudent(Student x) { x.setRollNo(2); x.setBranch("CSE AI ML"); System.out.println("Roll No " + x.getRollNo() + " Name " + x.getName() + " branch " + x.getBranch()); } } |
Output
1 2 3 4 5 6 | D:\Test Java>javac StudentDemo.java D:\Test Java>java StudentDemo Student Details Roll No 1 Name Ram branch CSE Roll No 2 Name Ram branch CSE AI ML Roll No 2 Name Ram branch CSE AI ML |
Passing array as argument in java method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class ArrayArgument{ public static void main(String[] s){ int[] numbers={4,5,1,6,2}; ArrayArgument ar=new ArrayArgument(); ar.multiply(numbers); System.out.println("\nnumbers in main\n"); for(int a:numbers){ System.out.print(a+" "); } } public void multiply(int[] arr){ for(int i=0;i<arr.length;i++){ arr[i]=arr[i]*2; } System.out.println("\nnumbers in multiply method\n"); for(int a:arr){ System.out.print(a+" "); } } } |
Output
1 2 | 2 4 |