Kevinb4940
Kevinb4940

Reputation: 13

Print the value of a Java constructor

I am a java beginner and I am trying to get used to objects. Is there anyway that I can print out the value of a constructor in main? How can I print out the value's Kevin,20? Thanks

public class MainConstructor {

    public static void main(String[] args) {

        ConstructorClass emp1 = new ConstructorClass("Kevin", 20);
    }
}

//Constructor Class

public class ConstructorClass {

    private String name;
    private int number;

    public ConstructorClass(String name, int number) {
        this.name = name;
        this.number = number;
        System.out.println("called");
    }
}

Upvotes: 0

Views: 24792

Answers (5)

The Law
The Law

Reputation: 334

Constructor is basically just another method (but for the love of what is holy, never say that during interview or even to your professor) so there is nothing wrong with doing this:

public class ConstructorClass {
private String name;
private int number;
public ConstructorClass(String name, int number) {
    this.name = name;
    this.number = number;
    System.out.println(name+" "+number);
    }
}

But this solution is really ugly and kind of "hotfixy". Better solution would be to have constructor to only get the values and have separate method to print what you want:

public ConstructorClass(String name, int number) {
        this.name = name;
        this.number = number;
}
void printNameAndNumber() {
        System.out.println(name+" "+number);
}

And use the class like this in your main

ConstructorClass c = new ConstructorClass("John",85)
c.printNameAndNumber();

Also some people like to handle this by going through hoops and loops and overriding ToString, but that is being too overzealous and there is really no benefit in your case (or any other primitive case).

Upvotes: 3

Kebab Programmer
Kebab Programmer

Reputation: 1219

If you want to properly print out those values, you should have getter methods set in your methods

Example below

public String getName(){
    return name;
}

public int getNumber(){
    return number;
} 

Then to print those values, you should then use methods toString() and method print() to display your values

Example

public String toString(){
   return getName() + " " + getNumber();
}

public void print(){
   System.out.println(toString());
}

Then in the class with the main method, you call your print method for that specific class

Example

ConstructorClass emp1 = new ConstructorClass("Kevin",20);
emp1.print();

Hope this helped, Enjoy :)

Upvotes: 0

Grenther
Grenther

Reputation: 476

Try using toString() in your class

public String toString() {
        return this.name + "," + this.number;
    }

and in your main just do emp1.toString(); to print it to your console

Upvotes: 3

jonk
jonk

Reputation: 1494

Add a toString() method to ConstructorClass:

public String toString() {
    return name + "," + number;
}

Then call this from main:

public static void main(String[] args) {
    ConstructorClass emp1 = new ConstructorClass("Kevin",20);
    System.out.println(emp1.toString());
}

Upvotes: 4

Rahman
Rahman

Reputation: 3785

public ConstructorClass(String name, int number) {
        this.name = name;
        this.number = number;
        System.out.println(name + "," + number);
    }

Upvotes: 2

Related Questions