sorry guys, keep getting the varible not initialized output from the compiler, and was wondering if anyone could help, program is as follows. any suggestions? can anyone explain the nature of the error to me as well?
[ Jess added UBB [code] tags to perserve whitespace ] [ August 23, 2004: Message edited by: Jessica Sant ]
variables declared inside the main functions are 'local' to the main function.local variables are need to be explicitly initialized ; instance variables or static variables on the other need not be explicitly initialized, they get implicitly initialized to there default vales....
There are several problems with this code, all of which I feel do not belong in a beginner's forum (gotta teach them the right way).
Let's correct them to save confusion: - "public int classVariable" should end with a semi-colon. - It should also be declared static because it is referenced from within a static context (a compile-time error will result otherwise). - It is poor form to use the String concatenation operator in the way it has been. - The assertion that "System.out.println(object1);" will display null is incorrect - it fails to compile.
Here is the revised code:
What this code sample attempts to do is distinguish between initialization of fields versus method-locals (a method-local is a variable declared within a method - and a field is one declared inside a class). When you attempt to use a local, the compiler enforces that it has been assigned. However, fields have a default value assigned during object initialization (note that this is distinct from object instantiation - but that's another story ).
when you make a class, there basically are two things it can have - instance variables, and methods. instance variables are variables that are not declared inside any method. Your class has none of those since every variable is inside your only method - main() has actualprice, discountprice, etc.
ANY variable declared inside a method is NOT initialized. it has NO value until you GIVE it one. so, you could fix your problem by writing: etc.
The other thing people are talking about are instance variables. These are variable that ARE defined in the class, but NOT in a method. These DO get a default value (usually 0 or some form thereof). So, the other way to fix your problem is to declare your variables OUTSIDE of your main() method, but INSIDE the class...
Does that help?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
An easy way for beginners to remember about variable/object initialization is like this. If the variable/object is declared inside of the class but outside of a method (this includes the main method) then it has a default value. If it is declared inside of a method (including the main method) then it has to be manually initialized. Basiclly all of your variables were declared inside of the main method so they have to be manually initialized.
Joined: Dec 06, 2001
Originally posted by Vlado Zajac: Only coe variable might not be initailized before it is read. Other variables should be ok.
In the original code, none of the variables are initialized before use. That is what the OP got a compiler error. The example I gave should give the OP the idea how to initialize the rest of the variables correctly.