What is the difference between public,private,protected and default access modifiers in classes in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between public,private,protected and default access modifiers in classes in java?

if I do private static class Apple{},then the apple class cannot be accessed by the main java class??

13th Jun 2017, 2:30 AM
suryapoojary
suryapoojary - avatar
2 Answers
+ 2
These access modiiers prevent other classes from using the data in any way they want. For example, you can make a variable private so that another class inheriting the other class can't just manually change the variable. So instead your main class that has the private variable can have a function to set the variable but it'll go through filtering before being set. This is just an example, you can't do much more..
13th Jun 2017, 3:04 AM
Ajay Gonzalez
Ajay Gonzalez - avatar