Java Developer Junior job interview FAQ: part 4

In this fourth part of our popular series, we continue to look at frequently asked questions that may arise during the first round of interviews for the position of Junior Java Developer. If you haven’t read the previous instalments, you can find them here:

When would you prefer a static method to a classic method in Java?

A static method in Java is a method that belongs to a class rather than to a particular instance of that class. This means that we can call static methods directly from a class without having to create an object of that class.

The main differences between static methods and classical methods are:

  • Declaration: static methods are declared using the keyword static before the return type of the method.
  • Calling: we don’t need to create a class object to call a static method. We call it directly by its class name.
  • Attribute access: static methods can only access static attributes of the class, as they do not have access to the object instance.

Limitations of static methods

Because static methods do not operate on class instance attributes, we should be aware that:
– a static method cannot directly refer to an instance attribute,
– a static method cannot directly call an instance method,
– derived class cannot override static methods,
– in static methods we cannot use the keywords this and super.

Static methods are used when the method does not need to change the state of the object or does not use instance variables. They are also used when we want to implement a method without creating an instance. So it is most often used in helper methods.

When would you use the final method in Java?

A final method is the name given to a method that cannot be overridden in derived classes. In this way, we achieve the prohibition of changing the implementation of this method.

The main differences between final methods and classical methods are:

  • Declaration: final methods are declared using the keyword final before the return type of the method.
  • Overriding: the final method retains its original implementation from the parent class and cannot be modified.

The use of final methods is useful when we are designing a class and want to ensure that a particular finished method cannot be modified further. The standard Java libraries are full of methods marked as final.

What is a singleton and what are its practical uses?

Singleton is a class design pattern that ensures that only one instance of a given class exists, and provides external access to that instance. The goal of this design pattern is to prevent multiple instances of a class from being created, and to ensure that all references to that class point to a single existing instance.

The main properties of the Singleton class are:

  • Private constructor: The singleton class has its constructor set to private, preventing the creation of new instances outside the class itself.
  • Private static instance: The singleton stores its single instance as a private static variable of that class.
  • Public static method: The singleton provides a public static method that provides access to the instance itself. This method checks whether the instance already exists and either creates it or returns it.

The Singleton design pattern is useful when we need to ensure that only one instance of a class exists throughout the program environment, for example, when working with shared resources, database connections, or objects containing global settings.

What is the role of a destructor in Java?

In Java, there is no directly equivalent concept of a destructor as in some other languages such as C++. In Java, the garbage collector takes care of automatically freeing memory, so there is no need to explicitly define destructors.

When creating objects in Java, there is no need to think about freeing or destroying memory. When an object becomes unusable and its reference is lost, the garbage collector automatically detects such unused objects and frees the memory they occupy.

In Java, it is possible to implement a finalize() method that is called just before an object is removed from memory by the garbage collector. However, this method is not the same as the destructor in other languages and has limited impact on memory management and resource deallocation. The use of finalize() is not recommended as it can have unpredictable behaviour.

Describe exception handling and explain why it is important.

Exception handling is a concept that allows developers to handle and manage exceptional situations or errors that may occur during the execution of a program. Exceptions can be caused by a variety of factors, such as incorrect input, file system problems, or inconsistent program behaviour.

Main exception handling blocks

The main exception handling blocks are used to catch and handle exceptions. We recognize the following:

  • try: contains code where exceptions may occur. This block must always be present when exceptions are handled.
  • catch: a block that handles the caught exception. Catch blocks can be multiple for different types of exceptions.
  • finally: a block of code that is executed regardless of whether an exception has occurred or not. This block is used to ensure that certain code is always executed, for example to free up resources.
  • throw: is used to throw an exception explicitly within the code. It is placed inside the try block.

Throwing exceptions: Exceptions can be thrown explicitly using the throw statement. This statement is used to indicate that an exception has occurred.

Exception handling is an important part of ensuring that the program does not crash unexpectedly and provides information about any errors that may have occurred.

If you’re a Java developer looking for a job, read more about Java developer salary and check out our job offers!

About the author

Jozef Wagner

Java Developer Senior

Viac ako 10 rokov programujem v Jave, momentálne pracujem v msg life Slovakia ako Java programátor senior a pomáham zákazníkom implementovať ich požiadavky do poistného softvéru Life Factory. Vo voľnom čase si rád oddýchnem v lese, prípadne si zahrám nejakú dobrú počítačovú hru.

Let us know about you