class Faltu
{
	public static void main(String aa[])
	{
		int a=4,b;
		if (a==4)
			b=5;
		System.out.println ("b="+b);
	}
}

Here it says : "variable b might not have been initialized", but in the following :

class Faltu
{
	public static void main(String aa[])
	{
		int a=4,b;
		if (true)
			b=5;
		System.out.println ("b="+b);
	}
}

Here it compiles perfectly. Why ?

Recommended Answers

All 2 Replies

Because in the second case the compiler knows that b will always have a value, but it does not know that in the first version (the compiler does not compute expression values).

Thanks !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.