Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,526 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,526 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.