0% found this document useful (0 votes)
4 views2 pages

Java Variable Initialization Errors

This document illustrates the importance of variable initialization in Java programs. It presents a program that is not working properly because the variable "numOfYears" is used in a calculation before being initialized. This causes a compilation error both when running the program from the command line and within the Eclipse IDE. Proper initialization of variables is necessary for a Java program to compile and run correctly.

Uploaded by

bellohales
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java Variable Initialization Errors

This document illustrates the importance of variable initialization in Java programs. It presents a program that is not working properly because the variable "numOfYears" is used in a calculation before being initialized. This causes a compilation error both when running the program from the command line and within the Eclipse IDE. Proper initialization of variables is necessary for a Java program to compile and run correctly.

Uploaded by

bellohales
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

//

//
//
//
//
//
//
//

*******************************************************************
[Link] By: Aiman Hanna (C) 1993 - 2015
This program illustrates the importance of variable Initialization
Notice that this program is NOT working. Why?
Key Points:
1) variable Initialization
*******************************************************************

public class Initialization1 {


public static void main(String[] args) {
double profit, principle = 1000, interestRate = 0.0625; // it is possible to initialize at creation time
int numOfYears;
// Calculate profit after the given number of years
// Notice that until this point, profit is still uninitialized
profit = principle * interestRate * numOfYears;
[Link]("The profit after " +

numOfYears + " years is: " + profit + "$.");

}
}
/* The result of compiling the program
// Using command line
==================
C:\comp248>javac [Link]
[Link]: variable numOfYears might not have been initialized
profit = principle * interestRate * numOfYears;
^
1 error

C:\comp248>
// Using Eclipse
=============
Exception in thread "main" [Link]: Unresolved compilation problems:
The local variable numOfYears may not have been initialized
The local variable numOfYears may not have been initialized
at [Link]([Link])
*/

You might also like