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 6 of 6

Thread: Instance Variable Won't Change!!

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Instance Variable Won't Change!!

    Good Evening Everyone,

    I am having trouble getting my instance variable named "invoiceNumber" to update as the class variable "numberOfInvoices" is incremented.

    Would appreciate any help.
    Thank You

    Invoice Class File
    package javatask1;
     
    public class Invoice {
        private static int numberOfInvoices = 0; //Counts total number of invoices made.
        private String companyName;
        private double amountDue;
        private String chargeDate;
        private int invoiceNumber;
     
        //CONSTRUCTORS
        public Invoice() {
            numberOfInvoices++;
            companyName = "";
            amountDue = 0;
            chargeDate = "";
            invoiceNumber = numberOfInvoices;
        }
        public Invoice(String coName, double Amount, String dateCharged) {
            numberOfInvoices++;
            companyName = coName;
            amountDue = Amount;
            chargeDate = dateCharged;
            invoiceNumber = numberOfInvoices;
        }
     
        //GET METHODS
        public static int getNumberOfInvoices() {
            return numberOfInvoices;
        }
        public String getCompanyName() {
            return companyName;
        }
        public double getAmountDue() {
            return amountDue;
        }
        public String getChargeDate() {
            return chargeDate;
        }
        public int getInvoiceNumber() {
            return invoiceNumber;
        }
     
        //SET METHODS
        public void setCompanyName(String coName) {
           companyName = coName;
        }
        public void setAmountDue(double Amount) {
           amountDue = Amount;
        }
        public void setChargeDate(String dateCharged) {
           chargeDate = dateCharged;
        }
    }

    Test Program
    package javatask1;
     
    public class JavaTask1 {
     
        public static void main(String[] args) {
            // TODO code application logic here
            Invoice myBill = new Invoice("SeaGraphics", 150, "06152013");
            System.out.println(Invoice.getNumberOfInvoices()); 
            System.out.println(myBill.getInvoiceNumber()); 
     
            Invoice myBill2 = new Invoice("SeaGraphics", 150, "06152013");
            System.out.println(Invoice.getNumberOfInvoices());
            System.out.println(myBill.getInvoiceNumber());
     
            Invoice myBill3 = new Invoice("SeaGraphics", 150, "06152013");
            System.out.println(Invoice.getNumberOfInvoices());
            System.out.println(myBill.getInvoiceNumber());
        }
    }

    Output
    run:
    1
    1
    2
    1
    3
    1
    BUILD SUCCESSFUL (total time: 0 seconds)


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Instance Variable Won't Change!!

    I don't see any problem in the updating of the variable.
    It looks like it prints out exactly what you told it to print out.
    Perhaps you thought you were printing something else?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Instance Variable Won't Change!!

    Hmm. I'll explain what I thought I was doing and you can tell me where I went wrong.

    'numberOfInvoices' is supposed to be a counter that increments every time an "invoice" is created, in order to keep a count of the total number of invoices. 'invoiceNumber' is supposed to be the unique number that identifies each invoice that is created, so that's why I put it in the constructor. I was thinking that every time an invoice is created, each new invoice will have a number assigned by the line I highlighted below.

    public Invoice(String coName, double Amount, String dateCharged) {
            numberOfInvoices++;
            companyName = coName;
            amountDue = Amount;
            chargeDate = dateCharged;
            invoiceNumber = numberOfInvoices;  //GIVES EACH INVOICE A UNIQUE NUMBER (OR SO I THOUGHT)
        }

    My expected output:
    1 //numberOfInvoices
    1 //invoiceNumber
    2 ''
    2 ''
    3 ''
    3 ''

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Instance Variable Won't Change!!

    Look closely at what you told the program to print, and compare that to what you really want to print, they are not the same.

  5. The Following User Says Thank You to jps For This Useful Post:

    wawawarrior (June 5th, 2013)

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

    Default Re: Instance Variable Won't Change!!

    [unnecessary post removed...]

  7. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Instance Variable Won't Change!!

    Oh crap I'm an idiot.. Thanks for your help!

    Invoice myBill = new Invoice("SeaGraphics", 150, "06152013");
    System.out.println(Invoice.getNumberOfInvoices());
    System.out.println(myBill.getInvoiceNumber());

    Invoice myBill2 = new Invoice("SeaGraphics", 150, "06152013");
    System.out.println(Invoice.getNumberOfInvoices());
    System.out.println(myBill2.getInvoiceNumber());

    Invoice myBill3 = new Invoice("SeaGraphics", 150, "06152013");
    System.out.println(Invoice.getNumberOfInvoices());
    System.out.println(myBill3.getInvoiceNumber());

Similar Threads

  1. Instance variable inside a method
    By mstratmann in forum Object Oriented Programming
    Replies: 20
    Last Post: May 5th, 2013, 07:46 PM
  2. What is the difference between instant variable and instance variable?
    By avistein in forum Java Theory & Questions
    Replies: 7
    Last Post: January 6th, 2013, 11:42 PM
  3. [SOLVED] While loop won't allow variable to change
    By Thor in forum Loops & Control Statements
    Replies: 13
    Last Post: October 9th, 2012, 08:35 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM