Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Objects and Classes

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Objects and Classes

    I made a Bill class that uses a Money class and a Date class. I used the driver bellow and noticed that after I change the amount and dueDate following the first set of println statements, that all three objects(bill1, bill2, and bill3) get the values. Is there any way to prevent this?

    public class MyDriver{
     
        public static void main(String [] args){
     
            //Construct Bills
     
            Money amount = new Money(25);
            Date dueDate = new Date(3, 30, 2007);
            Bill bill1 = new Bill(amount, dueDate, "The phone company");
            Bill bill2 = new Bill(bill1);                                 // bill created from a already made bill
     
            System.out.println("Bill objects output:");
            System.out.println(bill1);
            System.out.println(bill2);
     
            bill2.setDueDate(new Date(5, 30, 2007));                      // change the date of a bill
            amount.setMoney(31, 99);                                      // these two lines set ALL amount and dueDate values to the same. Why?
            dueDate.setDay(29); 
     
            Bill bill3 = new Bill(amount, dueDate, "The record company");
     
            System.out.println(bill1);
            System.out.println(bill2);
            System.out.println(bill3);
     
        }
    }

    This is my Bill class. If you need to see the Money and Date class let me know.

    public class Bill{
     
        //Variables/Data
        Money amount;
        Date dueDate;
        Date paidDate;
        String originator;
     
        //Methods
     
        public Bill(Money amount, Date dueDate, String originator){
           setAmount(amount);                
           setDueDate(dueDate);
           setOriginator(originator);
     
        }
     
        public Bill(Bill toCopy){
           setAmount(toCopy.getAmount());                
           setDueDate(toCopy.getDueDate());
           setOriginator(toCopy.getOriginator());
        }
     
        public Money getAmount(){
            return amount;
        }
     
        public Date getDueDate(){
            return dueDate;
        }
     
        public String getOriginator(){
            return originator; 
        }
     
        public boolean isPaid(){
            if(!(paidDate == null)){
                return true;
            }else{
                return false;
            }
        }
     
        public void setPaid(Date onDay){
           //if(dueDate > paidDate){
             //  System.out.println("The due date has passed");
           //}else{
               paidDate = onDay;
           //}
        }
     
        public void setUnpaid(){
            paidDate = null;
        }
     
        public void setDueDate(Date nextDate){
            dueDate = nextDate;
        }
     
        public void setAmount(Money amountGiven){
            //if(amountGiven > 0){    //how else should i be protecting this?
                amount = amountGiven;
            //}
        }
     
        public void setOriginator(String originatorGiven){
            originator = originatorGiven;
        }
     
        public String toString(){
            if(paidDate== null){
                if(originator == null){
                    return "An unknown company owes " + amount + " by the due date " + dueDate;
                }else{    
                    return originator + " owes " + amount + " by the due date " + dueDate;
                }
            }else{
                if(originator == null){
                    return "An unknown company owes " + " paid " + amount + " by the due date " + dueDate
                       + " it was paid on " + paidDate;
                }else{    
                    return originator + " paid " + amount + " by the due date " + dueDate
                       + " it was paid on " + paidDate;
                }
     
            }
        }
     
        /**
        public boolean equals(Bill toCompare){
            //null check
            if (toCompare == null){
                return false;
            }else{
                if(!getClass().equals(toComplare.getClass())){
                    return false;
                }else if(  //   ){               *************************************************************************
                    return getOriginator() == toCompare.getOriginator() &&
                           getDueDate() == toCompare.getDueDate() &&
                           getAmount() == toCompare.getAmount() 
                }
     
            }
        }
        */
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Objects and Classes

    after I change the amount and dueDate following the first set of println statements, that all three objects(bill1, bill2, and bill3) get the values. Is there any way to prevent this?
    Yes, don't change amount or dueDate.

    It is important to realise that these variables (and others like them in Java) are references (or pointers) to objects. If you pass such references around as you do by using them in the two constructors and, indirectly, in the copy constructor then everything ends up with references to one and the same amount, and one single due date. So when you change the amount of money of the object amount points to everything with a reference to that object will see the change.

    If you want another amount, construct a new Money object.

    ---

    This behaviour is quite normal. And it leads people to write classes in such a way that the amount of money represented by a Money instance can't be changed. (Countries have much the same attitude towards their paper currency.) Such classes are called "immutable" and are very common: methods are simply not provided to change their "state".

Similar Threads

  1. Polymorphorism with array objects of different classes?
    By EDale in forum Collections and Generics
    Replies: 7
    Last Post: April 14th, 2013, 03:54 PM
  2. Classes and Array of objects
    By dynasty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2013, 01:53 AM
  3. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 08:17 PM
  4. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 11:35 AM
  5. Creating classes with generic objects
    By rbt in forum Collections and Generics
    Replies: 2
    Last Post: April 23rd, 2012, 08:08 PM