Core Java Tutorial

Core Java Tutorial

What is Method in Java

Methods:

Methods add behavior to our class or helps us to write reusable code. Example calculating salary, printing employee information etc for our Employee class.
Example:
public class Employee {
 String name;
 int age;
 long ph:
  String email;
 void setData(String n, int a, long p, String e) {
  name = n;
  age = a;
  ph = p;
  email = e;
 }
 void display() {
  System.out.println(name);
  System.out.println(age);
  System.out.println(ph);
  System.out.println(email);
 }
}

 

public class Main {
 public static void main(String[] args) {
  Employee e1 = new Employee();
  e1.setData("raja", 22, 64564 l, "[email protected]");
  e1.display();
 }
}