Java Employee Class Examples
A class is a container that holds related data (variables) and code (methods).
A class is like a blueprint or design to create objects.
It is a user-defined data type that helps in organizing code.
Class एक container है जो संबंधित data (variables) और code (methods) को रखता है।
Class एक blueprint या design की तरह होती है जिससे objects बनाए जाते हैं।
यह एक user-defined data type है जो code को व्यवस्थित करने में मदद करता है।
Here we learn Employee class with fields, constructor, methods, and reading input.
यहां Employee class के साथ fields, constructor, methods और input पढ़ना सीखेंगे।
Example 1: Basic Employee Class
In this example, we are creating an Employee class, giving them some information (name, age, salary), and then printing that information to the screen.
इस उदाहरण में हम एक Employee class रहे हैं, उसे कुछ जानकारी (name, age, salary) दे रहे हैं और फिर वह जानकारी स्क्रीन पर दिखा रहे हैं।
public class Employee {
String name;
int age;
double salary;
public static void main(String[] args) {
Employee emp = new Employee();
emp.name = "Aryan";
emp.age = 24;
emp.salary = 40000;
System.out.println("Name: " + emp.name);
System.out.println("Age: " + emp.age);
System.out.println("Salary: " + emp.salary);
}
}
Output:
Name: Aryan Age: 24 Salary: 40000.0
Simple Employee class with fields and printing values.
सिंपल Employee class जिसमें fields और उनकी values print की गई हैं।
Example 2: Employee Class with Constructor
public class Employee {
String name;
int age;
double salary;
Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public static void main(String[] args) {
Employee emp = new Employee("Abhishek", 27, 50000);
System.out.println("Name: " + emp.name);
System.out.println("Age: " + emp.age);
System.out.println("Salary: " + emp.salary);
}
}
Output:
Name: Abhishek Age: 27 Salary: 50000.0
Uses constructor to initialize values.
Constructor से values initialize की गई हैं।
Example 3: Employee Method to Display Details
public class Employee {
String name;
int age;
double salary;
Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
public static void main(String[] args) {
Employee emp = new Employee("Aryan", 30, 60000);
emp.display();
}
}
Output:
Name: Aryan Age: 30 Salary: 60000.0
Has method to display details.
Details दिखाने के लिए method है।
Example 4: Reading Employee Details from Keyboard
import java.util.Scanner;
public class Employee {
String name;
int age;
double salary;
void readDetails() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter age: ");
age = sc.nextInt();
System.out.print("Enter salary: ");
salary = sc.nextDouble();
}
void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
public static void main(String[] args) {
Employee emp = new Employee();
emp.readDetails();
emp.displayDetails();
}
}
Output:
Enter name: Abhishek Enter age: 28 Enter salary: 55000 Name: Abhishek Age: 28 Salary: 55000.0
Reads Employee details from user and displays them.
User से Employee details पढ़कर दिखाता है।
Example 5: Multiple Employees Using Array
import java.util.Scanner;
public class Employee {
String name;
int age;
double salary;
void read(Scanner sc) {
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter age: ");
age = sc.nextInt();
System.out.print("Enter salary: ");
salary = sc.nextDouble();
sc.nextLine(); // consume newline
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many employees? ");
int n = sc.nextInt();
sc.nextLine();
Employee[] emps = new Employee[n];
for(int i = 0; i < n; i++) {
emps[i] = new Employee();
System.out.println("Enter details for employee " + (i+1));
emps[i].read(sc);
}
System.out.println("Employee Details:");
for(int i = 0; i < n; i++) {
emps[i].display();
}
}
}
Output:
How many employees? 2 Enter details for employee 1 Enter name: Aryan Enter age: 25 Enter salary: 45000 Enter details for employee 2 Enter name: Abhishek Enter age: 29 Enter salary: 58000 Employee Details: Name: Aryan Age: 25 Salary: 45000.0 Name: Abhishek Age: 29 Salary: 58000.0
Handles multiple Employee objects using array.
Array का use करके कई Employee objects संभालता है।