[HD Pg 181, Sec. 8.1.2 - returning-multiple-values-from-a-method]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 181, Sec. 8.1.2 - returning-multiple-values-from-a-method]

Post by OCAJO1 »

Expanding on the code in the book,

Code: Select all

class Name {
    String firstName, lastName;
    
    public Name listNames() {
        Name n = new Name();
        n.firstName = "first";
        n.lastName = "last";
        return n;
    }
    
    @Override
    public String toString (){
        return firstName+" "+lastName;
    }
}

Class TestName{

   public static void main(String [] args) {

              System.out.println(new Name().listNames());
    }
}
I was wondering how does one make use of n to print the first an last name, rather than spelling them out in the toString() method?

Thanks

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 181, Sec. 8.1.2 - returning-multiple-values-from-a-method]

Post by admin »

Sorry, I did not understand your question.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 181, Sec. 8.1.2 - returning-multiple-values-from-a-method]

Post by OCAJO1 »

I think I answered my own question, that the use of toString() override to return string representation of the object, has nothing to do with how a reference to that object is treated.

Which brings up another question,

public String toString()
return null; or
return " "; or
return ""; or
return firstName; or
return firstName+" "+lastName;

returns string representation of the entire object's holding.

Not withstanding the fact that the last choice have to do with whether the toString() should do the print formatting (that's not a good idea, is it?) or the formatting should be done within the printf() or print() of the calling method - which of the first four choices are preferred/good/proper/better programming return value coding in this case?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 181, Sec. 8.1.2 - returning-multiple-values-from-a-method]

Post by admin »

If there are instance fields in the class, then the toString method should generate and return a string that includes their values. So, return null, return "" etc (which are nothing but hardcoded values) are bad choices.

It is not a bad idea to generate a properly formatted string in toString because most of the time, it is used in log statements. Many standard Java classes such as ArrayList, return a nicely formatted string.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 181, Sec. 8.1.2 - returning-multiple-values-from-a-method]

Post by OCAJO1 »

So the last choice would be the best way to handle the formatting in this case?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 181, Sec. 8.1.2 - returning-multiple-values-from-a-method]

Post by admin »

yes, that's correct.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests