A
ankur
This code snippet will compile and run as given below:
public class TooSmartClass {
public static void main(String[] args) {
int weight = 10, thePrice; // Local
variables
// int weight = 10, thePrice = 0; // Local
variables
if (weight < 10) thePrice = 1000;
if (weight > 50) thePrice = 5000;
if (weight >= 10) thePrice = weight*10; // Always
executed.
// System.out.println("The price is: " + thePrice); // (1)
}
}
But will not compile as given below:
public class TooSmartClass {
public static void main(String[] args) {
int weight = 10, thePrice; // Local
variables
// int weight = 10, thePrice = 0; // Local
variables
if (weight < 10) thePrice = 1000;
if (weight > 50) thePrice = 5000;
if (weight >= 10) thePrice = weight*10; // Always
executed.
System.out.println("The price is: " + thePrice); // (1)
}
}
Error : C:\Java Files>javac TooSmartClass.java
TooSmartClass.java:10: variable thePrice might not have been
initialized
System.out.println("The price is: " + thePrice); // (1)
^
1 error
Why so ?? Because thePrice is not initialized in both the scenarios !
Thanks,
Ankur
public class TooSmartClass {
public static void main(String[] args) {
int weight = 10, thePrice; // Local
variables
// int weight = 10, thePrice = 0; // Local
variables
if (weight < 10) thePrice = 1000;
if (weight > 50) thePrice = 5000;
if (weight >= 10) thePrice = weight*10; // Always
executed.
// System.out.println("The price is: " + thePrice); // (1)
}
}
But will not compile as given below:
public class TooSmartClass {
public static void main(String[] args) {
int weight = 10, thePrice; // Local
variables
// int weight = 10, thePrice = 0; // Local
variables
if (weight < 10) thePrice = 1000;
if (weight > 50) thePrice = 5000;
if (weight >= 10) thePrice = weight*10; // Always
executed.
System.out.println("The price is: " + thePrice); // (1)
}
}
Error : C:\Java Files>javac TooSmartClass.java
TooSmartClass.java:10: variable thePrice might not have been
initialized
System.out.println("The price is: " + thePrice); // (1)
^
1 error
Why so ?? Because thePrice is not initialized in both the scenarios !
Thanks,
Ankur