Goldman Sachs Interview Question for Java Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

Static class is a Java class, which only contains static methods.
Ex: java.lang.Math
which contains lots of utility methods for various maths function e.g. sqrt().

No instance of this class exists. Only fields and methods can be directly accessed as constants or helper methods.

public final class ActionHelper {

    /**
     * private constructor to stop people instantiating it.
     */
    private ActionHelper() {
        // /this is never run
    }

    /**
     * prints hello world and then the users name
     * 
     * @param users
     *            name
     */
    public static void printHelloWorld(final String name) {
        System.out.println("Hello World its " + name);
    }

}

Singleton classes are those, which has only one instance during application life cycle. And usually have
private constructor and one static method to create the instance.
Ex: like java.lang.Runtime

Singleton Class is class of which only single instance can exists per classloader.

public class ClassicSingleton {
    private static ClassicSingleton instance = null;

    private ClassicSingleton() {
        // Exists only to defeat instantiation.
    }

    public static ClassicSingleton getInstance() {
        if (instance == null) {
            instance = new ClassicSingleton();
        }
        return instance;
    }
}

Some important Points:


1) Singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members).
2) A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded, leading to potential class loader issues.
Static class provides better performance than Singleton pattern, because static methods are bonded on compile time.
3) One difference between Singleton and static is, ability to override. Since static methods in Java cannot be overridden, they leads to inflexibility. On the other hand, you can override methods defined in Singleton class by extending it.
4) Static classes are hard to mock and consequently hard to test than Singletons, which are pretty easy to mock and thus easy to test. It’s easier to write JUnit test for Singleton than static classes, because you can pass mock object whenever Singleton is expected, e.g. into constructor or as method arguments.
5) If your requirements needs to maintain state than Singleton pattern is better choice than static class, because
maintaining state in later case is nightmare and leads to subtle bugs.
6) Singleton classes can be lazy loaded if its an heavy object, but static class doesn't have such advantages and always eagerly loaded.
7) Many Dependency Injection framework manages Singleton quite well e.g. Spring, which makes using them very easy.
8) Main advantage of Singleton over static is that former is more object oriented than later. With Singleton, you can use Inheritance and Polymorphism to extend a base class, implement an interface and capable of providing different implementations. If we talk about java.lang.Runtime, which is a Singleton in Java, call to getRuntime() method return different implementations based on different JVM, but guarantees only one instance per JVM, had java.lang.Runtime an static class, it’s not possible to return different implementation for different JVM.
9) Singleton object stores in Heap but, static object stores in stack
10) We can clone the object of Singleton but, we can not clone the static class object
11) Singleton class follow the OOP(object oriented principles) but not static class
12) we can implement interface with Singleton class but not with Static class.

- R@M3$H.N November 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

simply awesome...

- rydar December 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome

- rydar December 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static class InvalidClass {
//compilte time error: Illegal modifier for the class InvalidClass; only public, abstract & final are permitted
public static void main(String[] args) {

}

}

- R.K Goru July 26, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More