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 (2014) Basic Android Programming Introduction to Methods and Classes

Sri Krishna
Sri Krishna
1,348 Points

definition of method

Confused of this please help me clearly.

3 Answers

In the following class

public class MyClass {
    int mNumber;
    String mString;

    public void setNumber(int number) {
         mNumber = number;
    }

     public int getNumber() {
           return mNumber;
     ]

      public void setString(String string) {
             mString = string;
       }

       public String getString() {
              return mString;
       }
}

the functions setNumber, getNumber, setString, and getString are all methods belonging to the MyClass class. mNumber and mString are the member variable. So if I then do

private MyClass someVariable = new MyClass();
someVariable.setString("A string");

I am calling the setString method on the someVariable MyClass object. Hopefully this clears things up some more. If there's anything I can be clearer on, please let me know.

Sri Krishna
Sri Krishna
1,348 Points

yes now i am clear , thank you Mr. James.

You're welcome. Have fun with Android development.

Hello,

A method is a function that is associated with a class/object. In other words, it defines what a class should be capable of doing.

A method definition is the part of a method where we define the name and the parameters that it takes. For example

public void setName(String name)

would be a method definition. Please let me know if this answers your question or if there is more that i can clarify.

Sri Krishna
Sri Krishna
1,348 Points

yeah still if you could explain me with some examples, it will be useful please.. Thanks in advance.