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 Build a Simple Android App Improving Our Code Simple Refactoring: Using a New Class

Christos Peramatzis
Christos Peramatzis
16,428 Points

Why not make getFact() static?

Is there a reason we're not making getFact() static, and instantiating a FactBook instead? Maybe there are benefits to it that I'm not aware of, but making it static seems more memory efficient.

1 Answer

He is making use of encapsulation that is basically wrapping the data and behavior in a single unit "object" and just expose the data by offering public methods to access or modify the data "setters and getters", This approach gives the class "object" total control over what its stored in its fields and how to behave. Making the code more maintainable and flexible.

Basically, if your method does something that depends on the individual characteristics of its class it shouldn't be static, for example, try to imagine the situation in real life. How would you get the facts of a specific book if you don't have?