Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
dlpuxdzztg
8,243 PointsCalling a function from a function?
So I completed the final challenge for 'Java Objects' and I loved it! However, there was something in the challenge that confused me a little:
public void addPost(ForumPost post) {
System.out.printf("New post from %s %s about %s.\n",
post.getAuthor().getFirstName(),
post.getAuthor().getLastName(),
post.getTitle());
}
We're calling a function from a function? (getAuthor().getFirstName().) I never knew this was possible. Can someone explain what it does and why we do it? (In this case.)
2 Answers
Jacob Bergdahl
29,119 PointsCalling methods from withing methods is very common, and you'll be seeing it a lot as you move into more advanced stuff :) What you have here is method chaining. Here, you're first getting the Author object, and then the name of the author! Is there something in particular that you're confused with regarding method chaining that I can explain in more detail? :)
Thomas Nilsen
14,957 PointsWithout having looked at the code you're basing this on, I wrote an example which contains the same properties and methods.
class Author {
private String firstName;
private String lastName;
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
class ForumPost {
private String post;
private String title;
private Author author;
public ForumPost(String post, String title, Author author) {
this.post = post;
this.title = title;
this.author = author;
}
public Author getAuthor() {
return author;
}
public String getTitle() {
return title;
}
}
class Main {
public static void main(String[] args) {
Author author = new Author("Team", "Treehouse");
ForumPost forumPost1 = new ForumPost("My 1st post!" , "First", author);
ForumPost forumPost2 = new ForumPost("My 2st post!", "Second", author);
addPost(forumPost1);
}
public static void addPost(ForumPost post) {
System.out.printf("New post from %s %s about %s.\n",
post.getAuthor().getFirstName(),
post.getAuthor().getLastName(),
post.getTitle());
}
}
When you call getAuthor(), the instance of the Author class is returned. This means you can immediately call public functions from that class.
Mateusz Hyla
11,210 PointsMateusz Hyla
11,210 PointsAs I see here you call a method getAuthor() and then from it you call second method getFirstName(). In my humble opinion you cannot invoke one method from second in one class entity but I might be wrong. Nevertheless good luck my friend ;-).