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

Java Java Objects (Retired) Harnessing the Power of Objects Methods and Constants

Emmet Lowry
Emmet Lowry
10,196 Points

hey guys can someone explain to me methods and constraints

I dont really grasp what is happening and would appriciate any help thx.

1 Answer

John Steer-Fowler
PLUS
John Steer-Fowler
Courses Plus Student 11,734 Points

Do you mean methods and constants? Rather than constraints?

I don't know any Java, but both Methods and Constants both appear in Ruby which I do know about (If I am wrong please someone correct me)

Constants are variables that you know are never going to change. I would create a variable to hold someone's age, which will ultimately change at some time and the variable can then be reassigned with the person's new age.

A Constant is a variable that will never change. I would use a constant when I want to store a piece of information that can be called upon in a program but never altered. For example, in a program that returns information about myself, I would store my name in a constant, because my name is never going to change. But I would store my age in a normal variable, because I could then change it later as I get older.

Constants are useful (and should be used) because they will trigger an error in your code if you attempt to change them later in the program. This is a great safeguard against accidentally changing the value of a variable that you would not want to ever change in your program.

I hope this makes Constants clearer for you.