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

Whats the difference between a method and variable in Java?

I simply don't understand it! I've also read the 'Java Basics for Android Development' blog >.<

3 Answers

Lets take a real world example that will help you understand better.

Say you just bought your self a new dog from the pet shop. Now that you have him you want to Name it Tommy . Which is great. But at the current instance only you know that his name is Tommy, however rest of your family and your friends dont have a clue of what his name is. For your family the dog's name is still a mystery . Now if you want your family to call him tommy as well you will need to tell them his name. Keeping the same in mind, the Name of your dog is a variable which stores a particular kind of the information about the dog i.e. his name, called Tommy .

Methods -
Well its great that he has a name now. But you will probably want to train him to jump or do the lie dead trick. Now lets say u are teaching him how to jump. For this you you will first need his name ,i.e. tommy and also some additional action commands. This particular function or jump action is a method which when supplied with the correct information instructs your dog to jump.

(I know this is a silly example but I have a habit to learn things through real life instances. ) :)

On a more Java level

Variable is any user defined name that stores some information that can later be used in a program to reflect that value.

String name ="Tommy";

here name is a variable

Similarly method is collection of codes or actions that may or may not take additional parameters for e.g. int name and help perform a particular kind of functionality

public void jump( String name) 
{
//code to make tommy jump
}

here jump is a method or a collection of code that instructs the dog to jump

Hope that explains.

So that Joseph Kony doesn't get confused in the future it's worth noting a variable inside a method is referred to in OO speak as a property, rather than a variable.

It's not letting me edit my answer on my iPad. I worded what I said poorly. Not all variables in a method are properties. But in the example above, if we put the name variable in a respondsTo method for instance it would then be refered to as a property of the method.

A variable is a means of storing an arbitary piece of data for later use.

A method is a function which is attached to an object and can be called on it.

Thanks for the clear up! But i thought the data-type called 'int' can only hold numbers and not words?

See now you can also grab the mistakes in it. Your right it should be a string not an int. :) .. I'll edit that. Thanks