Results 1 to 3 of 3
- 09-28-2011, 10:22 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
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?
- 09-28-2011, 10:23 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 09-28-2011, 10:44 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
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
-
Can any one help me to understand the Code
By soomroimran in forum New To JavaReplies: 2Last Post: 04-28-2011, 09:23 AM -
Understand Code
By Quizzle23 in forum New To JavaReplies: 9Last Post: 03-07-2011, 10:07 PM -
need to understand code
By Masken2 in forum New To JavaReplies: 2Last Post: 02-17-2011, 04:21 PM -
understand the code
By prof.deedee in forum New To JavaReplies: 8Last Post: 11-11-2009, 02:43 AM -
Trying to understand this code
By new2java2009 in forum New To JavaReplies: 2Last Post: 09-09-2009, 07:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks