Results 1 to 3 of 3
  1. #1
    Kimomaru is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Trying to understand this code better

    Hello,
    I am working through some code in my Java book and am having a heck of a time understanding how the output is generated based on the following code;

    public class ArrayExample {
    public static void main (String [] args)
    {
    Banner x [] = new Banner [10];
    x [0] = new Banner ();
    x [0].favoriteProgram = "Java";
    x [0].display ();

    x [1] = new Banner ();
    x [1].favoriteProgram = "Visual Basic";
    x [1].display ();

    for (int row = 0; row < 2; row++)
    {
    System.out.println (x [row].favoriteProgram);
    }
    }
    }


    When you compile and run this code, you get the following output

    Banner's Constructor
    I love Java
    Banner's Constructor
    I love Visual Basic
    Java
    Visual Basic


    Can someone help me understand how the line "I love" for both Java and Visual Basic lines are generated? Where does it say that in the code?

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Trying to understand this code better

    You'd have to look in the favoriteProgram() method of the Banner class, which you haven't posted.

  3. #3
    Kimomaru is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Trying to understand this code better

    Oh, I see. It's referring to code in another class. This is what it looks like;

    public class Banner {
    String favoriteProgram;
    static int numberOfBannerObjects;

    public Banner ()
    {
    System.out.println ("Banner's Constructor");
    numberOfBannerObjects++;
    favoriteProgram = "Java";
    }

    public Banner (String param1)
    {
    System.out.println ("Banner's constructor");
    numberOfBannerObjects++;
    favoriteProgram = param1;
    }
    protected void finalize ()
    {
    System.out.println ("Banner's finalize");
    }
    public void howMany ()
    {
    System.out.println ("The number of Banner objects is " + numberOfBannerObjects);
    }
    public void display ()
    {
    System.out.println ("I love " + favoriteProgram);
    }
    }


    I think it's going to be a challenge to fully understand everything in the text until I start to implement them in my own projects. Thanks for the help IL :)

Similar Threads

  1. Can any one help me to understand the Code
    By soomroimran in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 09:23 AM
  2. Understand Code
    By Quizzle23 in forum New To Java
    Replies: 9
    Last Post: 03-07-2011, 10:07 PM
  3. need to understand code
    By Masken2 in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 04:21 PM
  4. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM
  5. Trying to understand this code
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 09-09-2009, 07:18 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
  •