Its that time again, where we go over some more java interview questions! Same as before, questions and answers will be posted here.
- How would you mark an entity package private in Java?
By default, package private is the modifier. There is no explicit modifier for package private. If a class has no modifier (the default, also known as package-private), it is visible only within its own package.
2. Can you change the contents of a final array? Can you give an example?
Yes you can! However, you need to be careful with how you change the contents since they’re is a right way and a wrong way of doing it. Take the following code for example.
final int[] array = new int [5]
array = new int[10];
This is the wrong way of changing the contents. As oppose to changing the actual content within the array, you are changing the location of memory for this variable using the keyword new. This throws an array because you cannot change the location of memory of a final variable. Instead, what you would want to do is this.
public class FinalArrayExample {
final int[] array = new int[5];
// allowed
void changeArrayContents(int i, int val) {
array[i] = val;
}
This code will compile because instead of changing the location in memory by creating a new location, we are instead in the same location of memory, only changing the content from within that memory location, which is permitted by final variables.
3. What are a few ways you can improve the memory footprint of a Java application?
Improving the amount of memory used is vital in software engineering and here are a few things you can do to improve the amount of memory your application is using and prevent memory leaks.
- Limit scope of local variables. Every time a method or class finishes its job, all references from that object are lost, making them eligible for garbage collection.
- Avoid finalizers if possible, will make application run slower and cause heavy performance issues. The reason why this happens is because of how Java’s garbage collection functions. Java’s GC works by wiping an entire block of memory at once. It collects copies of short lived objects, moves them into a storage block and wipes everything when it is no longer used. However, with finalizers, the GC cant wipe everything out. It needs to sort through the copies to figure out what is finalized and put the finalized objects into a separate thread for execution. Meanwhile, it isnt focusing on cleaning up objects efficiently, so it keeps the objects alive longer.
- Set variable references to null when they are no longer in use. This makes them eligible for garbage collection.
3. What is a Singleton class and what is one of the best ways to implement a Singleton class?
By definition, the singleton is a design pattern in which you only use a single instance of a class. It restricts the instantiation of a class to one object. In singleton classes, you use a getinstance() method instead of a constructor. Also as a general practice, us class name as method name while defining a method. Written below is how we create the singleton class.
// Java program implementing Singleton class
// with getInstance() method
class Singleton
{
// static variable single_instance of type Singleton
private static Singleton single_instance = null;
// variable of type String
public String s;
// private constructor restricted to this class itself
private Singleton()
{
s = "Hello I am a string part of Singleton class";
}
// static method to create instance of Singleton class
public static Singleton getInstance()
{
if (single_instance == null)
single_instance = new Singleton();
return single_instance;
}
}
We start off by creating a class, and that class will contain a private static variable of that class type. We will assign this variable null for now, but will be using it as part of the getInstance() method.
// static variable single_instance of type Singleton
private static Singleton single_instance = null;
We will then create a public variable (String in this case) and create a private constructor to initialize that variable.
// variable of type String
public String s;
// private constructor restricted to this class itself
private Singleton()
{
s = "Hello I am a string part of Singleton class";
}
Then we create a static method for the getInstance(), this will act as the “constructor” for the singleton class to create the instance of singleton class.
// static method to create instance of Singleton class
public static Singleton getInstance()
{
if (single_instance == null)
single_instance = new Singleton();
return single_instance;
}
This is everything we need for the singleton class, now we throw everything into the main method and output by instantiating the singleton class.
class Main
{
public static void main(String args[])
{
// instantiating Singleton class with variable x
Singleton x = Singleton.getInstance();
// instantiating Singleton class with variable y
Singleton y = Singleton.getInstance();
// instantiating Singleton class with variable z
Singleton z = Singleton.getInstance();
// changing variable of instance x
x.s = (x.s).toUpperCase();
System.out.println("String from x is " + x.s);
System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
System.out.println("\n");
// changing variable of instance z
z.s = (z.s).toLowerCase();
System.out.println("String from x is " + x.s);
System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
}
}
The following will ouput

In the Singleton class, when we call getInstance() method for the first time, it creates an object of the class with name single_instance and return it to the variable. Since single_instance is static, it is changed from null to some object. Next time, if we try to call getInstance() method, since single_instance is not null, it is returned to the variable, instead of instantiating the Singleton class again.
One of the best ways to implement a singleton class is to use an enum type because Java ensures that only a single instance of an enum is ever created, the singleton class implemented via enums is safe from reflection and serialization attacks. Heres an example
class Demonstration {
public static void main( String args[] ) {
Superman superman = Superman.INSTANCE;
superman.fly();
}
}
enum Superman {
INSTANCE;
private final String name = "Clark Kent";
private String residence = "USA";
public void fly() {
System.out.println("I am flyyyyinggggg ...");
}
}
Citation for code/references: https://www.geeksforgeeks.org/singleton-class-java/