aspose file tools*
The moose likes Beginning Java and the fly likes variable not initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variable not initialized" Watch "variable not initialized" New topic
Author

variable not initialized

solus hoo
Greenhorn

Joined: Aug 23, 2004
Posts: 2
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 ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The error means exactly what it says. You need to give an intial value for all variables that you declare. One way to fix this is to set them to zero. For example:

Another possible solution is to declare the variable when you make the calculations:

HTH

Layne


Java API Documentation
The Java Tutorial
Alain Boucher
Ranch Hand

Joined: Feb 25, 2003
Posts: 51
If you declare a variable in the class section, the initialization of those variables are automatic. but in a method it's not. Look this:

Ex:
public class MyClass{
public int classVariable
public static void main(String args[]){
int int1;
Object object1;

System.out.println(""+classVariable); //Display 0
System.out.println(""+int1); //Crash
System.out.println(object1); //Diaplay Null
}
}

This is java 101 man ; )


Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Originally posted by Alain Boucher:
This is java 101 man ; )


Which is why this forum's name has "(beginner)" in it...


Junilu - [How to Ask Questions] [How to Answer Questions]
Er.Ashish Gupta
Greenhorn

Joined: Aug 23, 2004
Posts: 4
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....
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Another possible solution is to declare the variable when you make the calculations

This is a good practice, the benefits of which Peter den Haan explained in a recent thread.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Only coe variable might not be initailized before it is read. Other variables should be ok.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


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 ).

Hope this helps.


Tony Morris
Java Q&A (FAQ, Trivia)
solus hoo
Greenhorn

Joined: Aug 23, 2004
Posts: 2
why is variable 'coe' not initialised? i keep getting that particular compiler error. and i'm sorry guys, i'm an absolute beginner.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Originally posted by solus hoo:
why is variable 'coe' not initialised? i keep getting that particular compiler error. and i'm sorry guys, i'm an absolute beginner.


Hello Solus. This is a rule given in Java Language Specification under 4.5.4 Initial Values of Variables. Only instance variable or array component is initialized with a default value.

Joyce
[ August 23, 2004: Message edited by: Joyce Lee ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
er...

Static class members are also automatically initialized.

I like to think about it thus: All members are automatically initialized. That's class members, instance members, and array members.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Solus... let me try...

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
Jay Will
Greenhorn

Joined: Feb 18, 2003
Posts: 2
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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable not initialized