Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: An Object Constructor Passing Same Object as Parameter

I have made an object called Transaction which I am passing in an ArrayQueue.

Here is the Transaction class constructors (I have the appropriate setter and getters too):

public class Transaction {

    private int shares;
    private int price;

    public Transaction(int shares, int price) {
       this.shares = shares;
       this.price = price;
    }

    public Transaction(Object obj) {
       shares = obj.getShares();
       price = obj.getPrice();
    }
}

In the second constructor there I am wanting a scenario where I can pass into it a different Transaction object that has been dequeue(ed) and get the info from that transaction and make it into a new transaction or possibly manipulate it before I put it back into the queue. But when I compile it does not like this.

Is this acceptable programming practice to pass a specific object into it's own object's constructor? Or is this even possible?

like image 584
Maggie S. Avatar asked Nov 15 '13 22:11

Maggie S.


People also ask

Can we pass object as parameters in constructor?

Defining a constructor that takes an object of its class as a parameter. One of the most common uses of object parameters involves constructors. Frequently, in practice, there is a need to construct a new object so that it is initially the same as some existing object. To do this, either we can use Object.

Can we pass object as argument to constructor in Java?

Passing Object as Parameter in Constructor One of the most common uses of objects as parameters involves constructors. A constructor creates a new object initially the same as passed object. It is also used to initialize private members.

When we pass object as a parameter to the constructor it is called as?

If you don't explicitly write a constructor, the compiler automatically inserts one for you. Parameterized Constructor – A constructor is called Parameterized Constructor when it accepts a specific number of parameters.

Is it possible for a constructor to have a same number of parameters?

A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.


2 Answers

It's called copy-constructor and you should use public Transaction(Transaction obj) instead of Object and also provide getters:

public class Transaction {

    private int shares;
    private int price;

    public Transaction(int shares, int price) {
       this.shares = shares;
       this.price = price;
    }

    public Transaction(Transaction obj) {
       this(obj.getShares(), obj.getPrice()); // Call the constructor above with values from given Transaction
    }

    public int getShares(){
        return shares;
    }

    public int getPrice(){
        return price;
    }
}
like image 159
Mariusz Jamro Avatar answered Sep 27 '22 22:09

Mariusz Jamro


You need to specify the same type:

public Transaction(Transaction obj) {
       shares = obj.getShares();
       price = obj.getPrice();
    }

Provided that you have defined getShares() and getPrice().

like image 27
Akira Avatar answered Sep 27 '22 22:09

Akira