This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes might not have been initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "might not have been initialized" Watch "might not have been initialized" New topic
Author

might not have been initialized

mike hew
Greenhorn

Joined: Oct 26, 2006
Posts: 27
Well I'm back, lol.
Thank you all for the great advise, has help alot.

Have all 3 programs written with only 2 Errors.

Can someone help explain the following to me?

-Called Compiler C:\PROGRA~1\Java\JDK15~1.0_0\bin\javac.exe-
-Target File: G:\NEW9~1\CheckingAccountsTest.java-
G:\NEW9~1\CheckingAccountsTest.java:98: variable currentAcccount might not have been initialized
sumOfBeginningBalances += currentAcccount.getBalance();
^
G:\NEW9~1\CheckingAccountsTest.java:130: variable currentAcccount might not have been initialized
sumOfBeginningBalances += currentAcccount.getBalance();
^
2 errors

-Finished-


Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
When you create an instance of a class, if you don't explicitly assign values to the instance variables, they are given default values.

However, this is not true with local variables. You have to make sure you initialize local variables before any statement that tries to use their value.
mike hew
Greenhorn

Joined: Oct 26, 2006
Posts: 27
So our you saying something like this,

CheckingAccount currentAcccount = new CheckingAccount(arguments); ?
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Hi.

I think that the problem is that the variable in question is a local variable and it is initialized inside of a conditional. I think that you need to initialize it in the main block of code for that method.

-- Kaydell
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
yes, local variables MUST be explicitly initialised before they're first read.
If you initialise them in conditional statements structured in a way that the compiler cannot ascertain one will always be executed, it will generate that error.
Either restructure your conditionals so the compiler can ascertain your local will always be initialised (if possible) or initialise it explicitly to something at declaration (most people use null for that).


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: might not have been initialized