Skip to main content

What is Method In Java

As we know Object is an entity with states and behavior , in java we are creating methods to represent behavior of object.


Method is defined as group of instructions grouped together to perform a task, one of the biggest advantage of method is re-usability.we can create a method in java and we can call that method many times to perform some task.We do not need to write same code again and again.We can execute a method by calling or invoking it.

One of the most important method in java is the main method ,execution of every java program , starts with main method :

How to write main method in java ?
	public static void main(String[] args) {
		
	}
Note - execution of every java program starts with main method.

- we can create our own method to perform some set of operations :

Method Signature In Java :

syntax :
AccessSpecifier ReturnType methodName(parameter1,parameter2){

}

NOTE - Method Name should be in Camel Case Format :
Example : addNumbers(),calculateInterest(),addition(), getEmployeeInformation() etc.

there are two main types of methods :

1.Predefined method.
2.User defined method.

1.Predefined methods are declared as part of java library , these are built in methods in java , which we can use as per requirement .Examples : toUpperCase(),toString(),length(),equals() etc.

2.User defined methods are created by programmer to perform some task.

Example of user defined method in Java :
	public int addTwoNumbers(int num1,int num2) {			
		return num1+num2;
	}	
method can be static or non-static , if method is available in another class we can call static method by using class name and we can call non-static method by using object of class.

Example of Method in same class in Java : 
now here our static method is available in same class ,so we can call it directly inside main method as in below, if method is not returning anything then we can use void as return type.
Example :
 public class MethodDemo {
    public static void main(String[] args) {
	addTwoNumbers(2,3);
    }		
    public static void addTwoNumbers(int num1,int num2) {
        System.out.println("Addition is :"+(num1+num2));
    }
   }
Output of above program :
Addition is :5
In above example both methods are static and are in same class, so we can easily call one static method in another directly. but what if addTwoNumbers() method is not-static ? we can call it using object , check below example : Example :
	
 public class MethodDemo {
    public static void main(String[] args) {
	 MethodDemo m1=new MethodDemo();
	 m1.addTwoNumbers(2,3);
    }
    public void addTwoNumbers(int num1,int num2) {
	System.out.println("Addition is :"+(num1+num2));
    }
 }
    
Output of above program is : Addition is :5 Note - if non-static method is available in another class , you can call method by creating object of that class. if static method is available in another class , you can call it directly by using class name. Example :

 public class CollegeApp {
	public static void main(String[] args) {
    	    Exam ex=new Exam();
     	    ex.doExamRegistration();
	    Exam.calculatePercentage();
	}	
 }	
 class Exam{
   public void doExamRegistration() {
	System.out.println("Enrolled for Exam");
   }	
   public static void calculatePercentage() {
	System.out.println("Percentage calculated");
   }
  }

Output is :
Enrolled for Exam 
Percentage calculated

Note : In above example in Exam class calculatePercentage is the static method , so we called it using class name, and doExamRegistration is non-static so we called it using object of class.

Popular Posts

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Stream In Java

stream in java 8 : Stream API is used to process collection of objects. stream() method is present in Collection interface from package java.util . Stream is an interface present in java.util package, filter() and map() methods are present in Stream interface. -->when to use filter()  method ? If we want to filter elements from collection based on some boolean condition then we can use filter() method.  for example  : filtering even or odd numbers from list of numbers. -->When to use map()  method ? If we want to create new object for every object in collection then we should use map() method. for example : if we want to increase every number in list by 2 , here every object is getting modified ,we have to use map() in this type of scenario. In below example , we used both filter() and map() methods on stream , inside filter method we applied some condition and in map method we are transforming our object to different one. package com.crtr4u.www; import java...

How much Software Engineer Earn in India

  No limit of earning for free lancer software developer : In service based companies - If Software Engineer work for same MNC from beginning of career then sometimes financial growth is low,still they can reach in between 7 lacs to 15 lacs in India. If programmer switch job after every 2-3 year and work for at least 5 years then they can earn minimum in between 14 lacs to 25 lacs for the position of Senior Software Engineer.  Being from a normal college , completed my education with average percentage , then  As 2 year experienced guy - I was getting offers of 8 lacs, 10 lacs  As 5 years experienced guy the offers are like  Minimum 12 lacs ,14 lacs, 18 lacs from Indian MNCs.  Money is  no bar for right candidate, if you are able to crack interview and if you can  write effective code then you can earn more than 20 lacs in India itself. Be motivated, improve your technical skills and yes you can earn great amount from Job too...  People with ...