Well I was working on a program using constructors for understanding it and I encountered a problem. Here is the code.Sorry if the code is too long.
import java.util.Scanner;
class Employee {
int id,sal;
String name;
String desig;
Employee()
{
id=001;
name="Paul";
desig="VirtualWorker";
sal=10000;
}
Employee(int def, String message)
{
System.out.println("How to fill in details(Example):");
System.out.println("ID:"+id);
System.out.println("Name:"+name);
System.out.println("Designation:"+desig);
System.out.println("Salary:"+sal);
System.out.println("Default value:"+def);
System.out.println("Default message:"+message);
}
void input()
{
Scanner in=new Scanner(System.in);
System.out.println("");
System.out.println("Welcome to SS Enterprises!");
System.out.println("What is the id?");
id=in.nextInt();
System.out.println("What's the name of employee?");
name=in.next();
System.out.println("What's the designation?");
desig=in.next();
System.out.println("What's the salary?");
sal=in.nextInt();
}
void print()
{
System.out.println("");
System.out.println("ID:"+id);
System.out.println("Name:"+name);
System.out.println("Designation:"+desig);
System.out.println("Salary:"+sal);
}
public static void main(String args[])
{
Employee emp=new Employee();
Employee emp2=new Employee(929,"Message in constructor.");
Employee emp3=new Employee();
emp3.input();
emp3.print();
}
}
when the values of the parameterized constructor is printed in the output, i don't get the values given in the default constructor. I get default values like 0 or null.
Is there any way I can take the values of the default constructor and use it in the parameterized constructor?
Thanks in advance