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

Java

non-static variable object cannot be referenced from a static context @ Java Data Structures Challenge

My question and problems are below the instructure. Here is instructure:

Challenge Task 1 of 1

The method getTitleFromObject will be called and passed a String and/or a com.example.BlogPost. Return the object type casted as a String if it is a String, and if it is the BlogPost type cast it, and return the results of the getTitle method.

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) {
    // Fix this return statement to be the correct string.
    return "";
  }
}

I tried a lot of ways. I got many errors but I figured out that "Editor wants String result" And I add if statement:

    if(obj instanceof String){
     String object = (String) obj;
    }

Also I define a variable as private static String object; and Editor said "Try again!". There was no more answer. After that I got this problem. My latest code is:

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 String object;             

  public static String getTitleFromObject(Object obj) {
    // Fix this return statement to be the correct string.
 /*   
     if(obj instanceof BlogPost){
     String obj = (String) obj;
    }
   */ 
    if(obj instanceof String){
     String object = (String) obj;
    }

    return object;

  }
}

Thanks for any insight!

PS: I want to know that why non-static variable object cannot be referenced from a static context?

5 Answers

A non-static variable can't be accessed from a static method as they don't have access to a reference to any instance of the class. As static methods are tied to the class itself, it wouldn't know which instance of the variable to use, if any even existed at the time.

For the challenge: When you're assigning obj casted as a String to the variable object, object is falling out of scope before the return value. You can either just return obj casted as a String right away without using a local variable, or move the declaration for object before the if statement inside the function.

Remember that non-static (instance) variables can't be accessed from static methods, and even if this was a non-static method, declaring this in non-static method:

String object = "";

In a class that has an instance variable also named object would cause it to be shadowed by the local variable you just declared.

I changed it and deleted static variable from top. I added this in method but now I have this problem:

./TypeCastChecker.java:25: error: cannot find symbol
    return object;
           ^
  symbol:   variable object
  location: class TypeCastChecker
1 error

Also I think I can't understand what was going on type casting with super class or child class. This is main problem, i guess.

Try thinking about this without using local variables:

If you know obj is a String:

return (String)obj;

Otherwise, assume it's a BlogPost:

return ((BlogPost)obj).getTitle();

Ahha! I got it. Thanks for helping!

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

First off, let's not worry about adding any code outside of the method. (I'll swing back in another answer to answer your PS question)

My guess is that the problem with that code is that you are declaring that object only in the if block. If the object added isn't a string, what happens, what is the variable object?

Try declaring the String outside, before the if statement. Then you can follow similar logic to check and see if it is a BlogPost and set your String variable to the result of the getTitle method.

Let me know if that unblocks you....

I think I'm done! I always think we need variables for making some actions in our codes. We know that is String and if it is not, we need to assume obj is a BlogPost (for reaching our getTitle method).

Thanks!

Craig Dennis I think the big stumper here was calling the method on a typecasted object.

I am wondering though, if the parameter is an object (obj), how could it be a String?

String is a subclass of Object. Since String is an Object, it can be safely up-casted to one when passed in as an argument. The Object reference (the argument in this case) will refer to a String object, but will not be able to access anything specific to the String class, a down-cast is required for that.

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) {
    // Fix this result variable to be the correct string.
    String result = "";
    if(obj instanceof String){
      result = (String) obj;
    }
    return result;
  }
}