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 trialMike B
10,424 PointsHow are private variables accessed from outside of the created class directly?
I thought the purpose of creating private variables was so that they couldn't be accessed directly. While we've been using the List interface, I've noticed a couple times that we are calling private variables from outside the class.
For example, in the SongBook.java class we are calling mSongs (private List<Song> mSongs;) but mSongs isn't in "SongBook.java"...it's in "Song.java"...shouldn't we be using the "getSongs()" method? Why does this work?
1 Answer
Benjamin Keller
20,763 PointsThat's not exactly what's happening. You're right, private variables can't be accessed outside the class. What's going on there is in SongBook.java we're creating a new list of objects that are the public Song class and calling it mSongs.
Mike B
10,424 PointsMike B
10,424 PointsOh...I've been associating the prefix 'm' with private variables. I wasn't paying attention that the variable itself wasn't private. Thank you