SRL Archive - General

Java is not setting the strings?


rj

2013-04-18 15:56:14

So I am just testing stuff out, and lets say I want to set a string to something, for example:

public class Party {
String Name;
String Event;
public void DisplayInfo( ) {
System.out.println("Happy" + Event + Name + "!");
}

public void setInfo(String PName, String vent) {
PName = Name;
vent = Event;
}
public static void main(String[] args) {
Party celebrate = new Party();
celebrate.setInfo("Officer Barbrady","Birthday");
celebrate.DisplayInfo( );
}

}

Lets make it display Happy Birthday Officer Barbrady!

But that won't work the output is: Happynullnull!

What am I doing wrong?

senrath

2013-04-18 15:57:51

You have it reversed. It should be Name = PName, not PName = Name.

rj

2013-04-18 16:11:24

From: senrath
You have it reversed. It should be Name = PName, not PName = Name.


Ahh TY

Richard

2013-04-18 19:00:45

If you want to save yourself having to create a new object, you can just add static modifiers to both variables and methods involved in the static reference. Just a little bit of useful java info :)

public class Party {

static String Name;
static String Event;

public static void DisplayInfo() {
System.out.println("Happy" + Event + Name + "!");
}

public static void setInfo(String PName, String vent) {
Name = PName;
Event = vent;
}

public static void main(String[] args) {
setInfo("Officer Barbrady", "Birthday");
DisplayInfo();
}
}