1

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

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Sarvesh
  • 11
  • 2
  • Your parametrized constructor doesn't make sense. A constructor is generally for *constructing* the object, not for printing. Sometimes people do things like print from a constructor, but that's not a good thing to do when learning. You should use it to set values, and print using a method after it has been constructed. You are not actually constructing anything from the parameters you pass and that's bad. – RealSkeptic Aug 04 '16 at 14:28
  • Thanks for the info. I get it now – Sarvesh Aug 09 '16 at 13:21

2 Answers2

0

You can call them like this:

Employee(){
    // This calls the other constructor with the default values
    this(defaultDef, defaultMessage); 
    // The rest of the default constructor 
}

Employee(int def, String message){
    // The rest of the parameterised constructor
}
Nicholas Robinson
  • 1,359
  • 1
  • 9
  • 20
0

You can explicitly call the default constructor from within:

EDITED

Employee(int def, String message)
{
    this();
    ...
Alim Özdemir
  • 2,396
  • 1
  • 24
  • 35