Java – Keywords

In this tutorial, we would examine various Java keywords beginning with Java Modifiers

We would cover the following.

  1. Java Modifiers
  2. Types of Variable
  3. Enums in Java
  4. List of Java Keywords
  5. The Interface Keyword

1. Java Modifiers

Modifiers are keywords used to add additional meaning or restriction to classes, methods, or variables. The two categories of java modifiers are

  • Access Modifiers: public, protected, private, default
  • Non-Access Modifiers: final, abstract, strictfp

2. Types of Variables in Java

As you already know, variables are memory locations for storing data. Names of variables are chosen by the programmer. Therefore, Java provide three different categories of variable that can be used

  • Local Variables
  • Class Variables (declared using the static keyword)
  • Instance Variable

3. Enums in Java

Another important keyword in Java is enum. You can use Enum to limit a the values of a variable to some predefined values. These predefined values are called enums.

For example, we may have an application that uses a variable color. Since you want color to be limited to only certain values, you can use enum to create a list of colors. As such, color cannot be assigned a value that is not actually a color.

An example is given in Listing 1.0. If you run the code, you could see that the output is green

//Your Testing Enum
public class EnumExample {
	enum color{red, blue, green, yellow, pink, orange}
	public static void main(String[] args) {	
		color myColor = color.green;
		System.out.println(myColor);
		color myColor = "yello";   //compiletime error!
	}
}

Listing 1.0: Example of the Enum keyword

In the example above, you can only assign to myColor, a value that is in the enum list. Therefore, if you try to assign any value not in the enum list to myColor, you will have an error

4. List of Java Keywords

You can find list of Java keywords in Table 1.0. You cannot use any of these keywords cannot be used to name identifier.

abstractassertbooleanbreak
bytecasecatchchar
classconstcontinuedefault
dodoubleelseenum
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhile

Table 1.0: List of Java Keywords

5. The Interface Keyword

Finally we would briefly learn about Interfaces. An interface is a class can only be inherited. You use interface keyword to create a class that needs not be instantiated.

An example of interface in Java is given in Listing 1.1 below

public interface Shape {
	public void render(); 
	
	public void getType();
}

Listing 1.1: Example of Java Interface

In the above example, we have created an interface called Shape. The two methods, render() and getType() are not implemented here. Therefore they must be implemented by a class that implements this interface. We would learn more about interfaces in subsequent lessons