简体   繁体   中英

Inner Class - Order of Print Statements -

I'm puzzled as to why my program prints statements in a certain order?

I have a Student class , inside which is an Inner Class of Address . The idea of the program is to first assign a Home Address to a Student Object, but then also assign a University / Term Time Address by utilizing the Inner Address Class.

The code is as follows:

Student Class (with Inner Address Class)

 public class Student {

    private String name;
    private Address homeAddress, uniAddress;

    public Student(String name, int houseNumber, String homeStreet) {
        this.name = name;
        homeAddress = new Address(houseNumber, homeStreet);
    }

    public String getName() {
        return name;
    }

    public Address getHomeAddress() {
        String s = "n/a";

        if (homeAddress != null) {
            return homeAddress;
        } else {
          //  System.out.println(s);
            return null;
        }


    }

    public void setUniAddress(int num, String add) {

        uniAddress = new Address(num, add);
    }

    public Address getUniAddress() {

        String s = "n/aa";

        //If uniAddress isn't set, 
        // then "n/aa" gets printed before anything else i/e toString() method - WHY?

        if (uniAddress == null) {
           System.out.println(s);
            return null;
        } else {

            return uniAddress;
        }
    }

    @Override
    public String toString() {

        return "NAME: " + getName() + "\n"
             + "HOME ADDRESS: " + getHomeAddress() + "\n"
             + "TERM TIME ADDRESS: " + getUniAddress();

    }

    // Inner Class
    public class Address {

        private int number;
        private String street;

        public Address(int no, String street) {
            number = no;
            this.street = street;
        }

        @Override
        public String toString() {
            //return name + "\n" + number + " " + street;
            return number + " " + street;
        }
    }
}   // more Student methods .. }

The TestStudent Class (with main method)

  public class TestStudent {

    public static void main(String[] args) {
        //Home Address
        Student s1 = new Student("Cathy", 21, "Smithfield Drive");
        //Uni Address
        s1.setUniAddress(72, "Nottingham Drive");



        Student.Address anotherAddress = s1.new Address(8, "Deerfield Way");
        // note the use of new

        System.out.println(s1.toString());


    }
}

The output is:

n/aa
NAME: Cathy
HOME ADDRESS: 21 Smithfield Drive
TERM TIME ADDRESS: null

(all on new lines)

If I do not assign a Uni Address to the Student (ie If I comment out the appropriate line in the main method - that calls the setUniAddress() method), I am curious then, as to why 'n/aa' from the getUniAddress() method is printed before the toString() method? (as above)

If I do call the setUniAddress() method the out put is:

NAME: Cathy
HOME ADDRESS: 21 Smithfield Drive
TERM TIME ADDRESS: 72 Nottingham Drive

(all on new lines)

Which seems to work as intended.

I'm also wondering how, instead of printing 'null' to the TERM TIME ADDRESS : (when setUniAddress() method isn't called), I could return the 'n/aa' in it's place - that is what I was attempting to do?

Thanks.

  1. getUniAddress() is called from the toString() which is why the n/aa is printed first.
  2. If you want to print "n/aa" as a default value - set it as a default value, for example, change the declaration to:

    private Address homeAddress, uniAddress = "n/aa";


@Override
public String toString() {

return "NAME: " + getName() + "\n"
     + "HOME ADDRESS: " + getHomeAddress() + "\n"
     + "TERM TIME ADDRESS: " + getUniAddress(); // <-- here you call getUniAddress() which 
                                                // is why "n/aa" is printed first

}

in getUniAddress() you have the following line which prints "n/aa":

System.out.println(s);

When the return statement in the toString() method is executed the get*() methods are called. Then a string is created and returned by the toString() method.

So the 'n/aa' is printed while calculating the string to return from the toString(), then the toString() method returns the string and the main method prints the output generated from toString().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM