Jahongir  Sabirov
Jahongir Sabirov

Reputation: 470

Optional in java 8 keep returning toString method from class

I have question about Java 8 from library java.util.Optional.ofNullable; I have written following code but I hope that it will return value of field of class. But I keep getting toString method from class. What might be cause of behaving this code.

import static java.util.Optional.ofNullable;

public class Main12 {

    public static String getName(User user){
        return ofNullable(user).map(User::getName).orElse("noFound");
    }

    public static void main(String[] args) {
        User user = new User();
        user.setName("ew");
        System.out.println(user);
    }

}

The result that I get from terminal is User{id=null, name='ew', age=0, roles=[]}.I hope that I will get value of name field from User class.

public class User {
    private Long id;
    private String name;
    private int age;
    private Set<Role> roles = new HashSet<>();

    public User(long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public User(){

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", roles=" + roles +
                '}';
    }
}

User Class field and methods. When I send null parameter of into method getName from Main12 class. I will get null String but orElse from ofNullable not react what I have written there. Please if you have some idea how to fix it let me know.I never get noFound String or real name of field value of Class.

Upvotes: 1

Views: 668

Answers (2)

Mark Bramnik
Mark Bramnik

Reputation: 42531

public static string getName(User user) has nothing to do with a method getName that you’ve define inside the User class. In fact they’re completely different

So in the main method you don’t even call the static getName Method with Optionals inside - so it won’t even run.

Now, toString is called because you call System.out.println(user). In this case Java automatically calls toString. That’s what happens.

Optional.ofNullable will “wrap” a reference to the object (even if its null) into the Optional wrapper.

I know, this could be a comment rather than the answer, but its kind of too long for comment.

Upvotes: 3

Peter
Peter

Reputation: 333

You are not invoking the static method you defined above your main anywhere. You could do the following in your User class:

public String getName() {
    return Optional.ofNullable(name).orElse("noFound");
}

I have to state that this is not a good practice though. If name is really optional, it should default to something sensible, like your Set does.

Upvotes: 0

Related Questions