-2

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());
  • 1
    I don't understand the question. Can you write some client code that uses this class? What's your expected vs actual output? – Daniel Kaplan Jan 09 '13 at 19:37
  • Looks like it's a typical bean that holds information about a street address. Nothing more than information storage and representation; I don't see any actual logic here. – Makoto Jan 09 '13 at 19:38
  • 2
    What do you mean by "indexed"? – Mechanical snail Jan 09 '13 at 19:38
  • @Assdasd Asdasdas What is the part you dont understand. However the answers below explain a lot about your issue. – Smit Jan 09 '13 at 19:51
  • @smit I'm silly and just added it now, sorry about that. – Assdasd Asdasdas Jan 09 '13 at 19:52
  • @AssdasdAsdasdas So I hope you got answer to your question. If not let us know. – Smit Jan 09 '13 at 19:54
  • @smit None of the answers provided really solve my problem, mostly my fault, I assume I just don't understand something fundamental about getter methods in Java. – Assdasd Asdasdas Jan 09 '13 at 19:57
  • @AssdasdAsdasdas I really dont understand your question. Please be specific where and what you dont understood. However getter are used to retrieve the value of variable which is setted by setter method or by constructor intializer. [Follow this link for getter to understand] (http://stackoverflow.com/questions/2036970/tutorial-on-getters-and-setters) – Smit Jan 09 '13 at 20:04

5 Answers5

1

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, and zip.

You can create or instantiate a new StreetAddress object like this:

StreetAddress streetAddress = new StreetAddress("Lincoln street", "New York", "New York", "1234");

This means that you now has an object that you can pass around your program that contains the address:

Lincoln street,

New York,

New York 1234

If you want to get some information out of your object you can use that getter methods. For instance if you want to grab only the street information from your object you can do this:

String street = streetAddress.getStreet();

Similarly if you want to update the street field in you StreetAddress object you can use a setter method like this:

streetAddress.setStreet("Washington ave");

Now your object will contain the street address:

Washington ave,

New York,

New York 1234

These getter and setter methods work the same way for each field in you object: street, city, state, and zip.

The final method:

String mailingLabel(){  return street + "\n" + city + 
      ", " + state + " " + zip; } }

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:

System.out.print(streetAddress.mailLabel());

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 getters and setters;

Hope this helps :)

Community
  • 1
  • 1
travega
  • 8,284
  • 16
  • 63
  • 91
0

Your constructor sets the four internal values; street, city, state, and zip.

Once constructed, the four variables are able to be retrieved with the getter methods.

The four variables are able to be modified with the setter methods.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

You sent us a simple java class with constructor that accepts 3 arguments and initializes private fields. So, when you call this constructor like

StreetAddress address = new StreetAddress("Brookin", "New York", "NY", "12345");

the appropriate fields will be initialized.

You class has also setters - methods that allow to change values of particular fields. For example if now you call address.setZIP("54321") the zip code will be changed.

I hope this helps.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

somewhere else you would say something like this:

myAddr = new StreetAddress("My Street", "My City", "My State", "My Zip");

then those values are stored in this object and you can get them back later...

anotherZIP = myAddr.getZIP();
Randy
  • 16,480
  • 1
  • 37
  • 55
0

The wording of your question is a bit unclear, but it seems that you're getting confused with regard to Declaration vs. Instantiation vs. Initialization.

At this point I would recommend reading a quick tutorial on how those come into play: http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

Pulling out some quick pieces that might help you focus your attention:

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

What you're seeing here("line eleven"):

void setStreet(String s) {
    street = s;
}

is Declaration of what the method expects to receive(a String). s and other attributes of StreetAddress are initialized here:

StreetAddress add = new StreetAddress("Cheese Island", "East Hemisphere", "The Moon", "99999999");

... and then the value is further changed here:

add.setStreet("Solar Flare");

In this last example, "Solar Flare" is the value used to initialize the String s variable inside of setStreet().

Dwight DeGroff
  • 600
  • 3
  • 9