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

why remove "public" in front of the String getFact method?

wouldn't removing "public" in front of the String getFact method remove the ability to call this method in the MainActivity class? My understanding was that we can only call the String getFact method in the MainActivity class if it's a public method?

1 Answer

saykin
saykin
9,835 Points

Not sure which course you're on, but by default, methods in Java are public within their own class and package. If you don't know what a package is yet (will be talked about in later courses), think of it as a folder.

Within that package, unless a method is specified private, any class can call on that Class' method. However, if you want a class to use a field and/or method within a class outside of it's own package(folder), it must be marked public.

So if the files and classes are in the same package (folder) you don't need to explicitly mark variables and methods as public.