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!

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

Java Java Data Structures Getting There Type Casting

Quinton Rivera
Quinton Rivera
5,177 Points

Completely lost with the first part of the challenge question 1

Completely lost, I used the logic if obj is a String, return obj which is casted to a string and nothing else. public static String getTitleFromObject(Object obj) { if(obj instanceof String){ return (String)obj;} else{ return "";}}

I dont think i understand the logic need help this is killing me

package com.example;

import java.util.Date;

public class BlogPost {
    private String mAuthor;
    private String mTitle;
    private String mBody;
    private String mCategory;
    private Date mCreationDate;

    public BlogPost(String author, String title, String body, String category, Date creationDate) {
      mAuthor = author;
      mTitle = title;
      mBody = body;
      mCategory = category;
      mCreationDate = creationDate;

    public String getAuthor() {
      return mAuthor;

    public String getTitle() {
      return mTitle;

    public String getBody() {
      return mBody;

    public String getCategory() {
      return mCategory;

    public Date getCreationDate() {
      return mCreationDate;
import com.example.BlogPost;

public class TypeCastChecker {
  I have provided 2 hints for this challenge.
  Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
  NOTE: You must set all the hints to false to complete the exercise.
  public static boolean HINT_1_ENABLED = false;
  public static boolean HINT_2_ENABLED = false;

  public static String getTitleFromObject(Object obj) {
    if(obj instanceof String){
    return (String)obj;}
      return "";}}

2 Answers

You need to set "result" equal to the String casted obj and then return result after the if statement.

Quinton Rivera
Quinton Rivera
5,177 Points

Thanks brother i hate when they add so much unnecessary stuff or comments about challenges such as the blogpost class and information about the method accepting blogpost it makes it very confusing to a newbie like myself

You're welcome :)

The blogpost class may help you on the next task. I think they add it in there for reference only. If you were going to instantiate an object wouldn't you want to know that the class exists first? And perhaps you might need to use methods from the class.