What happens after the compilation and execution of following code?

import java.util.*;

public class X {

    public <X> X(X x) {
        System.out.println("generic constructor");

    public X(X x) {
        System.out.println("simple constructor");

    public X() { }

    public static void main(String args[]) {
        X x = new X(new Integer(5));
        X x1 = new X(x);
In first constructor X is a generic type, not X object type, hence it will be invoked for all types except X, for which second constructor will be invoked

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback