Note that there are some explanatory texts on larger screens.

plurals
  1. POInner Class - Order of Print Statements -
    primarykey
    data
    text
    <p>I'm puzzled as to why my program prints statements in a certain order?</p> <p>I have a <code>Student class</code>, inside which is an <code>Inner Class of Address</code>. 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.</p> <p>The code is as follows:</p> <p>Student Class (with Inner Address Class)</p> <pre><code> 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 .. } </code></pre> <p>The TestStudent Class (with main method)</p> <pre><code> 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()); } } </code></pre> <p>The output is:</p> <pre><code>n/aa NAME: Cathy HOME ADDRESS: 21 Smithfield Drive TERM TIME ADDRESS: null (all on new lines) </code></pre> <p>If I do not assign a Uni Address to the Student (i.e. If I comment out the appropriate line in the main method - that calls the <code>setUniAddress()</code> method), I am curious then, as to why 'n/aa' from the getUniAddress() method is printed before the <code>toString()</code> method? (as above)</p> <p>If I do call the setUniAddress() method the out put is:</p> <pre><code>NAME: Cathy HOME ADDRESS: 21 Smithfield Drive TERM TIME ADDRESS: 72 Nottingham Drive (all on new lines) </code></pre> <p>Which seems to work as intended.</p> <p>I'm also wondering how, instead of printing 'null' to the <em>TERM TIME ADDRESS</em>: (when <code>setUniAddress()</code> method isn't called), I could return the 'n/aa' in it's place - that is what I was attempting to do?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload