Java: An Object Constructor Passing Same Object as Parameter

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;
    }
}

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().


Yes this is entirely possible.

public Transaction(Transaction other){
    shares = other.shares;
    price = other.price;
}

You do not need to call their getters because privacy only applies to other classes.