Class Declarations in Java

Introduction

As promised here is the continuation of Understanding Java Classes. We will mostly discuss about the production rule for Class Declarations in Java. If you recall, in the previous post I mentioned about the below rule:

NormalClassDeclaration:
ClassModifiersopt class Identifier TypeParametersopt Superopt Interfacesopt ClassBody

Do not worry, if you have no idea about what production rules of a grammar are. I will define them below and we do not need to know that as I will explain things in simple words. 🙂

Dissecting the Production

If you look at the above definition, we have got the following words:


ClassModifiersopt

It tells that there is an optional set of class modifiers which can appear in the beginning of a class declaration. Please notice the word set, which means more than one modifiers can coexist. However, there are few modifiers which cannot coexist. Below is a list of modifiers which can be treated as class modifiers.

  • Annotation : You might have noticed in most of the J2EE frameworks like Hibernate, JPA and Spring we use annotations like @Entity, @Component etc.
  • public : This is an access modifier as well, it is very much required to specify the accessibility of a class, when a class is declared with this modifier, it can be accessed in any package. It cannot co-exist with protected and private modifiers. This modifier can only be used with top level classes and member classes. It cannot be used with local classes (which are defined inside a method) or anonymous classes.
  • protected : Similar to public, this is another access modifier, a top level class cannot be declared protected. It has to be a member class enclosed within a class or enum. A protected member class can be accessed in any sub class of the enclosing class across any package.
  • private : Similar to public and protected, this is another access specifier and this can only be used with a member class enclosed within a class or enum. A private member class cannot be accessed anywhere outside the enclosing type.
  • abstract : This is a different class of modifier which defines the non completeness of the the class.. It states that the class is incomplete in terms of its definition or implementation. The user must make a specialize version of the class by extending the abstract class. No instance of this class can be created. Which precisely means that it is not possible to use this class without extending and providing a complete implementation.
  • static: A member class can be declared static and that can be accessed using the enclosing class name. Depending on its access specifiers it can be made visible at appropriate points in the program.
  • final : A class is declared final if we know that we do not want further specialization of the class. The class is completely defined and must not be interfered. For e.g. java.lang.String, java.lang.Math. These classes define certain behaviors and the JVM expects them to behave that way> The reason can be optimization, too specific code for the underlying JVM or completeness.
    I mean, if the value of PI is 3.1428 then it is kind of final, no one changes it. So if I define a class which derives PI and I do not want every one to create multiple instance of the class PI because no matter how many instances we have it will behave the same. In such a scenario, I would prefer to make this a final class.
  • strictfp : We will discuss this later, once I write a post on Floating Point and IEEE. This is rarely used and we can skip this for now.
class

This is a keyword in java, used to declare a class. All the named classes are declared using this key word. In Java classes are instances of the class java.lang.Class.

Consider the below example code:

Every time we use the keyword class, we are informing the compiler that we need an instance of type Class which is referred by a name which is the identifier (here City) of the class.

Identifier

It is the name given to the class, here on this instance of the java.lang.Class will be identified by this name. You can refer to this instance anywhere in the application by this name (of course if the access modifiers permit).

TypeParametersopt

A Type parameter makes sense only when we declare a Generic class. A generic class declares a Type variable which can be specified when we instantiate or declare a specialized version of the Generic Class.


For e.g.: Let us design an adder which is generic enough to add Integers and Floating Point numbers. One possible way of doing that is Creating a Generic Adder and then while creating the instances of the adder we can specify the type. Here is a small code example:

A type variable is an unqualified identifier used as a type in class, interface, method and constructor bodies. Once an instance of the generic class is created by supplying a type. The type is fixed for that instance.

Superopt

This symbol in the production means that a class declaration may include a super class which is optional. This means we can use extends Superclass but it is not a required construct.

This is true because we may not want all our classes to extend any class.

Interfacesopt

Similar to the super classes, a class declaration may include one or more interface and that too is optional. This means we can use implements SuperInterface1, SuperInterface2 but it is not a required construct.

ClasssBody

Class body is the block of code which is will be executed once the class is loaded. This includes all the members, initializer blocks and constructors.


Interfaces and Enums

Similar to class declaration we have productions for Interfaces and Enums. The keywords class is replaced by interface and enum.

No matter if we declare class, interface or enum all are instances of the class java.lang.Class in the Java Virtual Machine.

Conclusion

We understood the production for class declaration in this post.

So, what is a Production?
A production is a rule in a grammar which is used to define a language. Java is a language and as any other language it follows a grammar of its own.

The grammar defines certain rules and everyone using this language has to obey these rules. So, it is really important that we understand each of these rules clearly.

I have added few questions which can clarify basic concepts of Java. You can check that out on our Fb Page.

Don’t forget to subscribe to TechieMe to get updates on latest posts.