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

General Discussion

What is variable, method in Java?

Hi everybody,

I've just completed the basic java programming from treehouse and now I've just started the next step which is "Java objects". However, it got me thinking in my mind about what does variable mean in Java? what does methods mean in Java?

I'm still not clear enough about what those means although I have already passed the basic language of Java.

Can anybody help me?

Thanks a lot !

1 Answer

variables are simply pointers to a value you want to keep for later reference or to use for transformation. As I am more of a JavaScript background I will show an example using JavaScript. Let's say you want to set a greeting variable here's how you'd to it with JS

var greeting = "Hello world";

What this mean is that I am declaring a variable by the name of 'greeting' and I am setting it a value of "Hello world". so when I want to show my greeting to the user, I can simply write for example

console.log(greeting); // which would output the string in your browser console saying "Hello world" since you have save the value of "Hello world" in your 'greeting' variable. An equivalent would be : console.log("Hello world"), but then I would have to keep writing "Hello world" over and over again which is not good practice DRY (Don't repeat yourself) is the primordial programming principle.

For methods, think of method as if you had a big old red toolbox. In there, you have a hammer, a screwdriver and a saw. They all have a different function to execute different tasks (screwdriver to screw or unscrew and so on..). If you look back at my variable example, what we are doing to output the greeting variable to the console is reaching through the console object and executing the log task (which has the function of logging a variable into a string to the browser console). To finish with my toolbox metaphor, by using console.log you are likely doing : toolbox.screwdriver("I'm screwed").

I hope this is helping you, I don't program Java so it was hard to show you an example using Java.

P.S : Don't forget to categorize your forum questions with the subject you are resquesting help for as you will get more attention from the Java community within the site :)

Thanks for your answer! I will try to classify my questions next time :)