What is the result of the following code compilation and execution:

class Quizful {
    
    private Double x = 2;
    
    public static void multX(Quizful q, Double n) {
        q.setX(q.getX() * n);
    }
    
    public Double getX() { 
    	return x; 
    }
    
    public void setX(Double x) { 
    	this.x = x; 
    }
    
    public static void tripleValue(Double x) {
        x *= 3;
    }
    
    public static Quizful resetX(Quizful q) {
        q = new Quizful();
        return q;
    }
    
    public static void main(String[] args) {
        Double x = 4;
        tripleValue(x);
        Quizful q = new Quizful();
        multX(q,x);
        resetX(q);
        System.out.print(q.getX());
    } 
}

Actually this is case of Autoboxing and Unboxing. Changing to private Double x = 2.0; and Double x = 4.0; will fix situation

2015 Oct 2, 4:37:28 PM

line 3: error: incompatible types: int cannot be converted to Double private Double x = 2; ^ line 27: error: incompatible types: int cannot be converted to Double Double x = 4; ^

2015 Oct 2, 4:34:51 PM

Isn't it compilation error because of a static method trying to access non-static method?

2015 Oct 2, 2:45:26 PM

There is no int value.

2015 Oct 2, 2:43:57 PM

Some error in Explanation maybe.

2015 Oct 2, 2:43:40 PM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes