Jump to content

Wrong output when printing out from an array (JAVA)

CreepyPro
Go to solution Solved by Ominous,

Remove the static from:

private static String recipient;

Static means that recipient has the same value for all instances of the class.  You're overwriting the variable every time you called the constructor which is why it's always the name of the last person in the cards array.

I've made a program that's suppose to create greeting cards with the user name but if I print out the card from the array (using a for or for each loop) each card prints out with the name of the last card instead of the custom one for each.

For example if I try to do: 

GreetingCard[] cards = {new BirthdayCard("Mom"), new ChristmasCard("Harry"), new
				ChristmasCard("Noel"), new BirthdayCard("Judd Nelson")};
		
				for(GreetingCard c:cards)
					System.out.println(c+"\n");

I get 

Dear Judd Nelson,
Happy Birthday Judd Nelson!!! Hope you have a great year!

Dear Judd Nelson,
Merry christmas Judd Nelson!

Dear Judd Nelson,
Merry christmas Judd Nelson!

Dear Judd Nelson,
Happy Birthday Judd Nelson!!! Hope you have a great year!

But if I test it without creating them in an Array I get the right names.

BirthdayCard a = new BirthdayCard("Mom");
System.out.println(a.greeting());
BirthdayCard b = new BirthdayCard("Judd Nelson");
System.out.println(b.greeting());
HolidayCard c = new HolidayCard("Harry");
System.out.println(c.greeting());

 

 

 

 

(ChristmasCard and BirthdayCard are the same but with different messages)

package GreetingCard;

public class GreetingCard {
	private static String recipient;
	
	public GreetingCard(String rec) {
		recipient = rec;
	}
	
	public String getRecipient() {
		return recipient;
	}
	
	public String greeting() {
		return "Dear " + recipient + ",\n";
	}
	
	public String toString() {
		return greeting();
	}
}
package GreetingCard;
public class BirthdayCard extends GreetingCard {

	public BirthdayCard(String rec) {
		super(rec);
	}

	public String greeting() {
		return super.greeting() + "Happy Birthday " + getRecipient() + "!!! Hope you have a great year!";
	}

}

If you happen to know why this happens please tell me the reason and not just how to fix it. Thanks in advance

Link to comment
Share on other sites

Link to post
Share on other sites

Remove the static from:

private static String recipient;

Static means that recipient has the same value for all instances of the class.  You're overwriting the variable every time you called the constructor which is why it's always the name of the last person in the cards array.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Haas_ said:

Remove the static from:


private static String recipient;

Static means that recipient has the same value for all instances of the class.  You're overwriting the variable every time you called the constructor which is why it's always the name of the last person in the cards array.

Thanks for your help. I can't believe I've missed that, I've been looking at this thing for almost an hour lol

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, CreepyPro said:

Thanks for your help. I can't believe I've missed that, I've been looking at this thing for almost an hour lol

That hits close to home

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×