0
Answered

Temporary field - example

cristianandreigrigore 1 month ago updated 4 weeks ago 2

I might not get the temporary field definition right. As far as I understood, if there is a class User, defined as below, where fullName is passed in the constructor of the class, while lastName and firstName are only used internally, both firstName and lastName are temporary fields.

class User {
private String fullName; private String lastName; private String firstName; ... }

Is this a good definition of temporary fields?

GOOD, I'M SATISFIED
Satisfaction mark by cristianandreigrigore 4 weeks ago
Answered

Hi!

If first & last names are used internally, that doesn't mean that they would be temporary. Imagine a local variable in a method, say you keep some intermediary value in that variable between the method start and finish. If you convert that variable into a field, that would be a 100% temporary field. Of course, there's no reason to make such a conversion intentionally. However, such a field may be created after several iterations of changes to a method, for example, when the field value becomes no longer relevant in other methods of the class.

Hello! Thank you for your response, I understand your example.

class Person {
    private String name;
    private String officeAreaCode;
    private String officeNumber;

    public String getName() {
        return name;
    }
    public String getTelephoneNumber() {
        return ("(" + officeAreaCode + ") " + officeNumber);
    }
    public String getOfficeAreaCode() {
        return officeAreaCode;
    }
    public void setOfficeAreaCode(String arg) {
        officeAreaCode = arg;
    }
    public String getOfficeNumber() {
        return officeNumber;
    }
    public void setOfficeNumber(String arg) {
        officeNumber = arg;
    }
}

So in this class 

officeAreaCode

and

officeNumber

are considered temporary fields because they are used in the

getTelephoneNumber

method?