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 Harnessing the Power of Objects Constants

Does order of keywords in field declaration not matter? Also doesn't declaring private field final seem redundant?

Final private String characterName, yet public static final int MAX_PEZ?

Also what is the point of using private and final for characterName if public and final works sort of the same without the need for getters? Maybe I'm wrong but both can't be changed once initialized? Why do the extra work to create getters and setters with private fields if final keyword does the job?

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Arthur Kharzamanov !

Great questions!

First order doesn't matter, which is what I'm showing off there, glad you asked! There is a preferred formatting order, but you are right, they don't matter, except to what your team decides. I try follow the Google Java Style Guide and here's what they say about modifier order. They are referring to the Java SE Language Specification.

So the final method is powerful because it gives you a compile time check. Did you see how the code couldn't even run? It's a guarantee that it won't be changed.

It's all about expressing yourself and ensuring that the rules you set out are followed by users of your code. The private field can be changed within the same class code. So just like how my imaginary teammate Chris wrote the swapHead method in the Final video, you could have problems.

That make more sense?

Oh that's right, now I get it! Thank you, Craig!