1. Overview
In this article we'll see the most used keywords of the Java programming language and exaplain what they are meaning.
A Keyword is a command that contributes to defines the behavior of variables, methods and classes. In certain circumstances you can use more than one keyword for single member.
2. Access Modifier
By definition, the access modifier keywords defines who can access to the member. For member it means variables, methods and classes.
Type of access modifiers in Java:
- public: Anyone can access to the member declared as public.
- private: Only the other members of the same class can access to the private member.
- protected: The protected member is accessible by: the members of the same class, the other classes of the same package and the sub-classes of the other packages.
public class MyCalss {
private MyObject myObject = new MyObject();
//...
}
N.B. Top level classes can be only public.
3. Non Access Modifier
The most important Java Non Access Modifiers are: static, abstract, final, interface and synchronized. They defines the behavior and the scope of the members (variables, methods, classes).
Now we are going to explain them in details:
static
In Java the variables declared as static are allocated into the static memory, also called metaspace. It means that only one instance for the static variable will be created and it will be shared over all the instances of the class. All the instances, allocated in the heap space, will have a pointer to the same static member in the metaspace. You can access to the static variables directly through the name of the class, without allocate an instance of the class. The static variables can be declared only at class level, not inside the methods.
The static methods are usually used to do operations that are indipendent from an instance creation, for example using the static methods you could declare an Utility class and use it without allocate the instance (es. Collections, Math, StringUtils, BeanUtils). The static methods are resolved at complie time, for this reason they can't be overrided (because the override is a runtime operation). They can't be abstract and can't uses the keyword this or super because as we said they don't referrs to a specific instance.
In Java only the nested classes can be declared static. The nested classes can be access to the static members of the external class, private included.
abstract
The abstract keyword can be used on methods and classes. An abstract class may include (or not) abstract methods, but if a class include abstract methods must be abstract, it can't be used to create instances, to do this it's necessary a class that inherit form the abstract class and implements all her abstract mehtods. The abstract methods doesn't have a body, and they can be declared only in an abstract class or in a interface.
public abstract class MyAbstractCalss {
public abstract void myAbstractMethod();
//...
}
interface
The interface keyword being used for a special type of classes that contais only abstract mehtods or static and final variables and methods. Infact by default the fields of an interface are public static and final and the methods are abstract. The interfaces can't be instanciated, can't contains constructors and a class that implements an interface must implements all his abstract methods.
There are difference between abstract classes and interfaces: you can extend only one class, that it is or not abstract, instead you can implement any number of interfaces. With abstract classes, you can declare fields that aren't static and final, and define public, protected, and private concrete methods. Insead with interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public.
final
The final keyword may be used on variables, methods and classes. The value of a variable declared as final can't be changed once initialized, the final methods can't be overrided and the classes can't be inherited.
synchronized
To avoid race conditions in multi-thread context, Java provides the synchronized keyword. Use this keyword means only one thread at time can access to the code that use it, indipendently by the number of instance of that class. Synchronized can also be used over static code.
4. Conclusion
We have seen the main keywords and access modifier of Java programming languege, in total it has 52 keywords. You can find here the entire list.