Skip to main content

Java methods

Methods represent the business logic of the application and it defined inside the class.
Ex: I have a java class, in that class, I want to do add two numbers so we should keep this logic of addition in methods.

  • The method should have a signature and signature represents method name and parameters.
  • A java class contains multiple methods.
syntax
<access modifier> <return type> <method_Name>(parameters){
//method body
//business code
}
Access modifiers for  methods:
Access modifiers applicable to methods are public, private to check the visibility of methods to other classes.
Return type:
The return type is the data type of the value returned by the method, if the method doesn't return value, return type value should be void.
Method_Name:
The method name is any legal identifier.
Parameters:
Parameters should placed in parenthesis() by keeping(;).if there is no parameters ,we must use use empty parameters.
Naming a method
Method name is any legal identifier. method name should be the verb in lower case.if there is multi-word method name that begins with lowercase and first letter of each second word must capital letter.
How to invoke methods?
In java we have two types of methods 
  • Instance methods
  • Static methods
Instance methods:
Instance methods are  invoked by using Object reference  a class.
package com.practice;
public class Instance_Method_Demo {
 int x = 30;// instance variable declaration
 int y = 30;// instance variable declaration
 // instance method
 public void add() {
  System.out.println("sum of numbers:" + (x + y));

 }
 public static void main(String[] args) {
  // Object creation
  Instance_Method_Demo instance_Method_Demo = new Instance_Method_Demo();
  instance_Method_Demo.add();// invoking instance method with object
         // reference }

}
output
sum of numbers:60
Static methods:
Static methods are invoked by using class name like below
<classname>.<static_method>
Example
package com.practice;
public class Static_Method_Demo {
 static int x = 30;// static variable declaration
 static int y = 30;// static variable declaration
 // static method
 public static void add() {
  System.out.println("sum of numbers:" + (x + y));

 }

 public static void main(String[] args) {
  // invoking static method by using class name  Static_Method_Demo.add();
 }

}
output
sum of numbers:60
Example  for method with parameters
package com.practice;

public class Static_Method_Demo {
 static int x;// static variable declaration
 static int y;// static variable declaration
 // static method
 public static void add(int x,int y) {
  System.out.println("sum of numbers:" + (x + y));

 }

 public static void main(String[] args) {
  // invoking static method by using class name  Static_Method_Demo.add(30,30);
 }

}
output
sum of numbers:60

Popular posts from this blog

Java Primitive DataTypes

Java data types will tell us what type of data can variable hold. Ex: int x = 10; In the above variable, x can hold int data type value.so we must declare any variable with data type before we can use a variable.

Instance Variables in Java

Instance variables should declare with in the class but outside of any method or block or constructor.instance variables can accessible inside class all methods , blocks & constructors.

Angular 9 Date Pipe Example

Table of Contents what is pipe in angular? Angular Date Pipe Pre-defined Date Format Custom Date Formats what is pipe in angular? In Angular Pipes are used to transform data.Pipes will take input data and transforms input data in desired output. Symbol of Pipe : " | " Angular Date Pipe Angular DatePipe is one of built-in Pipes.DatePipe is used for Format a date value according to locale rules. Syntax {{value_expression| date [:format[:timezone[:locale]]]}} DatePipe avalibale in '@angular/common' module. Example //app.component.ts import   {   Component   }   from   '@angular/core' ; @ Component ({   selector:   'app-root',    template:   `{{dOB | date:'fullDate'}}`,    styleUrls:  ['./app.component.css'] }) expo...