|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
Hello,
I'm new to Java so bear with me please . In my main method I'd have something like this:Java5 Code:
I'm using Eclipse and it is telling me to initalize my local variables. I want them to have no value or 0 before I use them. I can obviously set the as "= 0" when I declare them but it seems tedious, especially if I have a lot of variables. Is there a way I can a) make it so they don't need to be initialized? or b) set it so they all initialize as zero? Thanks for any suggestions, explanations or help! ![]() |
| Sponsored Links |
|
|
|
|||||
|
When ever you use a variable it needs to be initialized or else it will throw a null pointer exception. However you can initialize multiple variables just as you initialize a single variable:
Java Code:
Other than that, there is no other way to do it.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
you have to initialize the local variables if you want to use.
You maynot initialize the instance variables. |
|
|||
|
In the object class you could have a default constructor that initializes the variables to 0. E.g.
public class objectName { int var1, var2, var3; public static objectName() { var1 = var2 = var3 = 0; } public static objectName(int var1, int var2, int var3) { var1 = var1; var2 = var2; var3 = var3; } } |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Geometry Circle-Line Inters. | Maurice_Z | Programming Theory | 0 | 11-28-2007 01:30 PM |
| Global variables (SERVER) | Jaan | PHP Tutorials | 0 | 03-22-2007 09:43 PM |
| Variables | clookid | PHP Tutorials | 1 | 01-11-2007 08:40 PM |
| Java:Tutorial - The Variable | John | Java Tutorials | 0 | 12-09-2006 09:59 AM |
Goal: 100,000 Posts
Complete: 68%