Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

might not have been initialized

 
mike hew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So our you saying something like this,

CheckingAccount currentAcccount = new CheckingAccount(arguments); ?
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic