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

Now add a String field (member variable) named mTitle. Make it public.

how do i complete the task above...i have tried to code like public class PictureBook{ public String mTitle(){ } } ...But it brings an error

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

3 Answers

When you're declaring a variable you don't use parenthesis and curly brackets those are for methods. You were close:

public class PictureBook{
  public String mTitle; //This is a variable you close it with a semi-colon

}
anil rahman
anil rahman
7,786 Points

What you have coded is a method not a variable:

public class PictureBook{
  public String mTitle(){ //method
  }
}

He states member variable named mTitle and make it public like this:

public class PictureBook {
    public String mTitle; //member variable
}
Dekel Zoaretz
Dekel Zoaretz
14,864 Points

Every statement that ends with parentheses is a method. that code that you have written is a method that returns a string and I don't think that what you want. The way to do it right is like Derek Markman wrote. In order to distinguish local variables and global variables it is a good practice to prefix the local variable with the char 'm'.