I know what all of these do except for abstract. I'm currently in the process of teaching myself java with what I consider a middle-school-level education (my highschool was in a bad neighborhood so I got shafted)...
But what exactly are the usage patterns for these keywords? When do I use what? When do I omit them? Putting 'public' in front of my classes makes every class that uses it require a new file, can I just omit that if I want to create a monolithic source file?
Every bit of information I look up, explains exactly WHAT these do, just doesn't give a clear view of when/why/where I should use them.
Thanks in advance, Anthony
In order to understand the when/why/where of these keyword uses, you have to get a grasp on some key concepts of Object Oriented Programming and Java. I suggest looking into Encapsulation and Polymorphism.
Also off the top of my head, I believe 'public' is implied so it isn't required but its good practice to have it there.
Bozho has covered the uses for the keywords pretty well, but I will add that if you do not declare a scope at all, your scope becomes package-private, which means that anyone in the same package as the class can use that class/method. Basically, it's more permissive than
private
, but less permissive than justprotected
, asprotected
allows access from outside a package.Information about the 'no modifier' access here:
I recommend going through the Java tutorial:
And also take a look at the book questions if you want to explore more of Java:
private
,public
andprotected
are all used for declaring the Scope of a class for variable.static
means that the thing being defined is a member of the class and not an object that is instance of the class.abstract
means that the class can not directly created and can only be used by subclasses. An abstract method can be defined in an abstract class and means that any subclass must define a method matching the defined signature.final
means that the variable can only be assigned a variable once at its creation. A final class/method can not be inherited/overridden, respectively.Stay away from putting everything in one big file. Use an IDE, like Eclipse, and it will make it easy to work with code that has one class per file. It allows you to better organize your code and encapsulate code so you don't end up in a situation where everything knows about everything. This will lead to errors as it becomes easier to accidentally use something the was created to do a different purpose.
For beginners, here are my rules of thumb:
I would strongly suggest you fight the urge to use one monolithic source file. Try to keep methods shorter than one screenful, and classes shorter than 300 lines.
Sources tell what do these keywords mean because when/why/where they are used follows from that. My explanations have the "when" word, for example, but they follow directly from the semantics of the keywords.
private
should be used when something is not used outside of a given classprotected
should be used whenpublic
is used when something is accessible by every other classThe above three are "visibility modifiers". They are used when you want to limit the usage of some methods/fields/classes to a group of objects, and hide them from other objects. There is another visibility modifier - the default one (when no other is present). It is used when you want your class/method/field to be accessible only to classes from the same package.
static
is used when you don't need an instance of a class (i.e. object) to use it:abstract
when you don't want to provide implementations in the current class:final
- when you don't want something to change.