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

Thread: referencing variables from multiple instances of a class?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default referencing variables from multiple instances of a class?

    Hello,

    I'm new to Java, and reading Intro to Java Programming for Dummies, and I was just wanting to clarify something about creating and referring to objects. This is my first post, so please forgive any formatting blunders..

    Here is the code (from pg. 333):
    import java.util.Scanner;
     
    class ProcessPurchasesss {
     
       public static void main(String args[]) {
          Scanner myScanner = new Scanner(System.in);
          Purchase aPurchase;
     
          for (int count = 0; count < 3; count++) {
             aPurchase = new Purchase();
              System.out.print("Amount: ");
             aPurchase.amount = myScanner.nextDouble();
             System.out.print("Taxable? (true/false) ");
             aPurchase.taxable = myScanner.nextBoolean();
     
             if (aPurchase.taxable) {
                aPurchase.total = aPurchase.amount * 1.05;
             } else {
                aPurchase.total = aPurchase.amount;
             }
     
             System.out.print("Total: ");
             System.out.println(aPurchase.total);
             System.out.println();
          }
       }
    }

    My question is -> if one creates multiple objects all assigned to the same variable (in this case 'aPurchase') how does one refer to each individual object variable? so if I referred to aPurchase.total, I'd only get the last assigned value right?

    many thanks!
    Nick


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: referencing variables from multiple instances of a class?

    Your understanding is correct. As with any variable, it will store the last item assigned to it.

    Multiple distinct objects can be stored in arrays or other collections. The array is likely the first you'll learn, so I'll use that as an example. The statement:

    Purchase[] purchases = new Purchases[10];

    creates an array called 'purchases' that is sized to store 10 Purchase objects.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: referencing variables from multiple instances of a class?

    It's not so much that multiple objects are assigned to the same variable, but rather that each individual object created has it's own unique variable values and such.

    Like Greg said, you can use an array to create multiple purchase objects and access them like that. To access any object within the array Greg wrote above, you would write:

    int selectedIndex = 0; //Which ever purchase object you would like to refer too from the array
     
    int newPurchaseTotal = purchases[selectedIndex].total; //ex, if you wanted to store it in a new variable

Similar Threads

  1. Log location of multiple instances of jboss...
    By rathi in forum Java Servlet
    Replies: 2
    Last Post: January 23rd, 2012, 10:46 PM
  2. Replies: 0
    Last Post: January 17th, 2011, 05:14 AM
  3. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 08:42 AM
  4. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM
  5. Multiple instances of linked list
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 07:48 PM