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

Android Build a Simple Android App Improving Our Code Creating a New Class

how to add a String field named mTitle. ./PictureBook.java:3: error: cannot find symbol return mTitle.

What do I return if there is as yet no code in mTitle?

PictureBook.java
public class PictureBook {
public String mTitle() {
  return mTitle;
 }
};

1 Answer

andren
andren
28,558 Points

A string field is not a method, you have essentially created a getter method. Getter methods are used to return the content of private member variables, but they are not themselves member variables. A member variable is as the name suggest a type of variable. It is a variable that exists inside the class rather than inside a method.

Creating a member variable is done in a similar fashion to creating a normal variable. The only real difference is that member variable have an access level (public, private, etc) that you have to set just like methods and classes do.

Creating a public string member variable looks like this:

public class PictureBook {
    public String mTitle;
}

Thanks Andren. In other words a string doesn't take a function, it's just a string.

andren
andren
28,558 Points

More or less, though this is not just about strings it's the same for all member variables. Whether they are Strings, ints or any other type of variable.