Java: What the f ! (The confusion that is Java)
Java

Java: What the f ! (The confusion that is Java)

Java is one of the world’s most important and widely used computer language. It has kept that spot for many years. Much of the modern world runs on Java. It is still the first choice of many developers for developing web-based and Android applications.

Image result for java memes

But, for some time, keeping all that praise aside, let’s talk about the confusion that is Java. The Java language comes with a large set of 50 keywords. A keyword is a word that is reserved by a programming language because the word has a special meaning in that language. 

Following are all the Java keywords:

abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instance of return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

As the title suggests, today we’ll be talking about the three f-words, which look pretty similar but have really different functionality, which can be kind of confusing sometimes. Actually, two of them are keywords and one is a method name. They are:

1: final
2: finally
3: finalize()

So, let’s talk about “What the f” each one of them one by one.

1: final

The keyword final has three uses. The first is related to declaring constants, whereas the other two are related to the object-oriented programming principle of inheritance. We will discuss them one by one:

Creating named constants

In this context, the final is to Java, what const is to C/C++.

The value of a variable that has been declared as final cannot be modified in any other part of the program, thus essentially making it as constant. Here’s an example:

final int MS_DHONI_JERSEY_NO = 7;

In addition to instance variables, as shown above, both method parameters and instance variables can be declared final. Declaring a parameter as final prevents it from being changed within the method. Declaring a local variable as final prevents it from being assigned a value more than once.

final Prevents Inheritance

In object-oriented programming, if a class inherits from another class, it means it gets functionality and properties that inherited class. In Java the way of doing it is like this:

Following is a pictorial representation of the same 😅:

Related image

But sometimes, you will want to prevent a class from being inherited. To do this, precede the class with a final. Here is a final class:

final Prevents Method Overriding

When a class inherits another class, it generally gets all the methods defined in the parent class, except the private methods. A private method will remain private to the class. It is not accessible by any code outside its class, including the subclasses.

In a class, when a method in a subclass has the same name and type signature as a method in its superclass, then the subclass method is said to override the superclass method.

While method overriding is a very powerful feature of the Java language, there may be cases, where we want to prevent it. To prevent a method from being overridden, precede the method declaration with the final as a modifier. Methods declared as final cannot be overridden.

method overriding java memes

The following example illustrates the final:

Because meth() declared as final, it cannot be overridden in B. Attempting to do this results in a compilation error.

2: finally

finally is one of the five keywords – try, catch, finally, throw and throws – that together make up the exception handling mechanism of Java.

An exception is a runtime error. Exception handling is a powerful feature because it provides you with a sophisticated method to handle runtime errors in your code.

Basically, in exception handling, we try to do something in our code. If an exception is thrown by some method called in our code, we catch that exception.

When exceptions are thrown, the flow of the execution of the code moves from the regular path into the catch block. Depending upon the code in the catch block, at times it can also happen that a method returns prematurely. It can be really dangerous at times. Suppose that the terminated method opens a file. Now if the method fails to code that file on exiting, that can lead to data corruption. So, we will want the code that closes the file to be bypassed by the exception-handling mechanism. The finally keyword is designed to address this problem.

finally creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. The finally block is also executed just before a method returns.

Several use cases of Java’s finally clause can be found here.

The finally clause is optional, although each try statement requires at least one catch or finally clause.

3: finalize()

Sometimes an object needs to perform some action when it is destroyed. Examples include the closing of a file previously opened by the object, closing a network socket etc. We want to make sure that such resources are freed before the object is destroyed. To handle such situations, Java provides a mechanism known as finalization. This mechanism allows specific actions to be taken when an object is about to be destroyed by the garbage collector.

To add a finalizer to your class, define the finalize() method. The method is called by the garbage collector right before an object is destroyed and the memory occupied by it is freed. The method contains those tasks – as mentioned above – that must be performed before the object is destroyed.

The finalize() method has this general form:

The keyword protected is an access modifier that limits access to finalize().

So, this was all about the three special words of Java that appear rather similar but are very different in functionality. And it’s not all. There are a lot of such confusing special words in Java, about which we will talk in the coming posts.

References:

 

Leave a Reply