So I’m still a fledgling programmer and I would really appreciate it if someone could explain to me how exactly the code below functions.
I’m aware without some methods I didn’t list here the code will not function but what baffles me starts at line eleven : String street is given the value of a variable that isn’t indexed (to my knowledge) and yet I’m still able to return it with the value intended and not valueless s.
How is it exactly that String s and the other variables serving the same purpose don’t modify the value of the variable I end up returning?
Once again I’m thankful for any help I get on this.
public class StreetAddress {
String street, city, state, zip;
StreetAddress(String s1, String c, String s2, String z) {
street = s1;
city = c;
state = s2;
zip = z;
}
void setStreet(String s) {
street = s;
}
String getStreet() {
return street;
}
void setCity(String c) {
city = c;
}
String getCity() {
return city;
}
void setState(String s) {
state = s;
}
String getState() {
return state;
}
void setZIP(String z) {
zip = z;
}
String getZIP() {
return zip;
}
String mailingLabel() {
return street + "\n" + city + ", " + state + " " + zip;
}
}
The class above would receive the information below and return a formatted label.
StreetAddress add = new StreetAddress("Cheese Island", "East Hemisphere", "The Moon", "99999999");
System.out.println(add.mailingLabel());
add.setStreet("Solar Flare");
add.setCity("Corona");
add.setState("The Sun");
add.setZIP("00000000");
System.out.println(add.mailingLabel());
This is a java class that describes the concept of a StreetAddress. Basically the class describes a real life object which has properties that make sense for that object. In this case you street address contains information the
street,city,state, andzip.You can create or
instantiatea new StreetAddress object like this:This means that you now has an object that you can pass around your program that contains the address:
If you want to get some information out of your object you can use that
gettermethods. For instance if you want to grab only the street information from your object you can do this:Similarly if you want to update the
streetfield in you StreetAddress object you can use asettermethod like this:Now your object will contain the street address:
These
getterandsettermethods work the same way for each field in you object:street,city,state, andzip.The final method:
Allows you to return a formatted version of your object similar to the examples above. You can print this formatted version of your object to the console by doing this:
In Java you use objects to describe real world concepts and in your program you can create many objects like this StreetAddress one to carry information around. Objects and their data are stored in memory while your program is running and not in a database (unless they are EJBs but forget about that for now). If you pass an object between classes it will maintain the data in it until you alter it again with
gettersandsetters;Hope this helps 🙂