Sunday, September 6, 2009

Puzzle 50 – Solution

Declaring the variable breadth as a
static variable solves the problem of forward reference in the puzzle. Static code is referenced and initialized before any instance code – and so the forward reference problem of the breadth variable gets solved.

package com.twister;

public class Area {

int length = 10;
int area = length*breadth;
static int breadth = 20;

public static void main(String[] args) {
Area a
= new Area();

A neat trick pointed out by TheMalkolm solves the problem using just 5 characters!

No comments:

Post a Comment

Solution for this question?