Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Output printed in wrong order?

    Chapter 6 in my book is using a sample class to help explain how to return an object from a method. Here is that sample class:

    Java Code:
    package chapter6;
    import java.util.Scanner;
    
    public class ReturnObject
    {
       public static void main(String[] args)
       {
          // Declare a variable that will be used to
          // reference an InventoryItem object.
          InventoryItem item;
    
          // The getData method will return a reference
          // to an InventoryItem object.
          
          item = getData();
    
          // Display the object's data.
          System.out.println("Here is the data you entered:");
          System.out.println("Description: "
                             + item.getDescription()
                             + "  Units: " + item.getUnits());
    
       }//end of main method
    
       /**
        * The getData method gets an item's description 
        * and number of units from the user. The method 
        * returns an InventoryItem object containing    
        * the data that was entered. 
        */
       
       public static InventoryItem getData()
       {
          String desc;   // To hold the description
          int units;     // To hold the units
          
          // Create a Scanner object for keyboard input.
          Scanner keyboard = new Scanner(System.in);
    
          // Get the item description.    
          System.out.print("Enter an item description: ");
          desc = keyboard.nextLine();
          
          // Get the number of units.
          System.out.print("Enter a number of units: ");
          units = keyboard.nextInt();
    
          // Create an InventoryItem object and return
          // a reference to it.
          return new InventoryItem(desc, units);
       }
    }

    And after I ran the file, this is the output I got:


    run:
    Enter an item description: hammer
    Enter a number of units: 45
    Here is the data you entered:
    Description: hammer Units: 45
    BUILD SUCCESSFUL (total time: 12 seconds)

    As you saw in the code, the main method was declared above the getData method, so why wasn't the output like this instead:


    run:
    Here is the data you entered:
    Description: hammer Units: 45
    Enter an item description: hammer
    Enter a number of units: 45
    BUILD SUCCESSFUL (total time: 12 seconds)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Output printed in wrong order?

    The position of a method relative to another method doesn't tell you anything; look at your main( ... ) method: before it prints anything it calls the getData() method and that method also prints something ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Output printed in wrong order?

    Quote Originally Posted by JosAH View Post
    The position of a method relative to another method doesn't tell you anything; look at your main( ... ) method: before it prints anything it calls the getData() method and that method also prints something ...
    You mean that Netbeans/Java is smart enough to know that it needs input from the user before it can pass values to the getData() method?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default Re: Output printed in wrong order?

    Nope. First, nothing passes values to the getData() method. It is simply called to obtain user input.

    Upon execution of the program, main is entered. And before the print statements in main are executed, the getData() routine is called. So those print statements and other code are executed first. Then upon return of the getData() method, the item contents are displayed.

    And like Jos said, the order of methods within a class has nothing to do with the order in which methods are called.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Replies: 10
    Last Post: 01-11-2013, 08:16 PM
  3. input 3 numbers output order least to greatest
    By rizowski in forum New To Java
    Replies: 8
    Last Post: 01-14-2011, 09:07 AM
  4. Wrong Order in Program Run
    By HeatR216 in forum New To Java
    Replies: 0
    Last Post: 03-25-2010, 08:41 PM
  5. Replies: 0
    Last Post: 10-14-2009, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •