This video gives fundamental insight about the usage of
Static variables and static methods in Java programming
For the purpose we have used employee and employee manager class to show the functioning.
The code that has been shown in the video is put down here for your reference. Thanks.
Dr. P Thendral
package trial;
import java.util.Scanner;
class Employee
{
int employee_id;
String name;
Employee()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
employee_id = Employee_manager.getEmployeeId();
}
}
class Employee_manager {
Employee[] emp_list = new Employee[5];
static int emp_id_gen=400;
static int emp_count = 0;
void create_new_employee()
{
emp_list[emp_count] = new Employee();
emp_count++;
}
void display_all_emp_ids()
{
for(int i=0;i(less)emp_count;i++) {
System.out.print(emp_list[i].name);
System.out.println(emp_list[i].employee_id);
}
}
static int getEmployeeId()
{
emp_id_gen++;
return emp_id_gen;
}
}
---------------------------------------------------------
package trial;
public class Intro {
public static void main(String[] args)
{
Employee_manager mgr = new Employee_manager();
mgr.create_new_employee();
mgr.create_new_employee();
mgr.create_new_employee();
mgr.create_new_employee();
mgr.create_new_employee();
mgr.display_all_emp_ids();
}
}
Output :
Enter your name
shiva
Enter your name
ram
Enter your name
lakshman
Enter your name
sam
Enter your name
kailash
shiva401
ram402
lakshman403
sam404
kailash405
Ещё видео!