Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variable not initialized

 
solus hoo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alain Boucher
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ; )
 
Junilu Lacar
Bartender
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alain Boucher:
This is java 101 man ; )


Which is why this forum's name has "(beginner)" in it...
 
Er.Ashish Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only coe variable might not be initailized before it is read. Other variables should be ok.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
solus hoo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jay Will
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic