• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

variable not initialized

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ; )
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only coe variable might not be initailized before it is read. Other variables should be ok.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic