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

Felix Putra
Python Web Development Techdegree Student 1,927 PointsWhat is variable, method, class, objects?
This is my first time using Java and I've reached java objects (sequence of java basics), but still don't understand what variable, method, class, objects and so on mean. I've been doing pretty great on the challenges and quizzes from Treehouse but I still consider myself as "not understand yet".
Help please!
2 Answers

Luke Glazebrook
13,564 PointsHey Felix!
A variable is a way of storing data under a specific variable name that you are going to be using in your program. For example, if you are going to be using your name a lot in a program then you could set a variable called "name" and set it's value equal to "Felix" so then instead of typing "Felix" when you are wanting to print out your name you can just type name. Also, this then allows you to change the name in one place and then it changes throughout the whole program.
String name = "Felix";
A method is a group of instructions which you can collect together to execute. For example if you had a function called sayHello() that printed out some welcome statements then instead of typing out the statements inside the method overtime you could just call the sayHello() function and it would do it for you.
public static void sayHello() {
//Enter The Commands You Want To Run Here!
}
A class is a basic building block of an object-oriented language and contains certain information and behaviours that instances of that class (objects) will be able to use. For example, if you had a class called Animal you could then create some methods within it, for example Sleep(), which each instance of that class can then use. So for example if you created an instance of the Animal class called 'dog' then you could use .sleep() and it would execute the code within the sleep() method of the Animal class.
Animal dog = new Animal();
dog.sleep()
Finally, an object is an instance of a class. In the last example that I gave you 'dog' is an example of an object as it is an instance of the Animal class. Once you have instantiated (created an instance of) an object you can then go ahead and use the methods and variables that the class contains on that object.
I hope that this helps you out in some way Felix! If you do need some more help on this then feel free to ask me anything you want to know! I know that this can be a lot to take in at one time.
Have fun programming and keep up the good work!
-Luke

Allan Clark
10,810 PointsVariable: This is a similar concept in pure math (except better). Variable is just a box that holds data. This data can be anything; number, letter, or Object.
//this creates an empty integer box
int iAmNumber;
//this puts the value 492 inside that box
iAmNumer = 492;
Method: This is a group of code instructions that we give a name so we can use those instructions over and over without having to write the same lines again. (remember DRY). Bender probably has a method like this in his coding.
public void benderDefault() {
console.printf("I am Bender, please insert girder");
}
*in case you're not a Futurama fan https://www.youtube.com/watch?v=d4JBFPFDEUo
When we get into Class and Object we cross over into the wonderful world of Object Oriented Programming (OOP).
Objects: Objects are everything. A car, a house, a bike, Bender these are all Objects. We model an Object in code by defining its state (variables) and behavior (methods). A car's state can be things like make, model, color, or if it is running. Behavior would be driving, crank, or turn off.
Class:
A Class is like an Object factory, it is what we use to define and create Objects. Say for the Car class that is where we define what state and behavior a Car has.
Example of a Car class:
public class Car {
//state aka variables
String mMake;
String mModel;
boolean mIsOn;
//class constructor creates and returns a Car Object
public Class(String make, String model) {
mMake = make;
mModel = model;
mIsOn = false;
}
public void crankUp() {
if (mIsOn == false) {
mIsOn = true;
}
}
}
Example of calling that class's constructor to create an Object:
Car myCar = new Car("Mazda", "RX8");