Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-2007, 07:54 PM
MojoMonkey's Avatar   
MojoMonkey MojoMonkey is offline
Newbie
 
Join Date: Dec 2007
Location: Ireland
Age: 19
Posts: 4
Rep Power: 0
MojoMonkey is on a distinguished road
Post Do I have to initialize my local variables?

Hello,

I'm new to Java so bear with me please . In my main method I'd have something like this:

Java5 Code:
  1. public class Example
  2. {
  3. public static void main(String[] args)
  4. {
  5. Constructor constructor;
  6. int var1, var2, var3;
  7.  
  8. constructor = new Constructor(var1, var2, var3);
  9.  
  10. }
  11. }

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-02-2007, 12:44 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,815
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

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:
  1. int var1, var2, var3 = 0;

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-02-2007, 07:06 AM
MojoMonkey's Avatar   
MojoMonkey MojoMonkey is offline
Newbie
 
Join Date: Dec 2007
Location: Ireland
Age: 19
Posts: 4
Rep Power: 0
MojoMonkey is on a distinguished road
Default

cool, thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-14-2007, 05:47 AM
satya5321 satya5321 is offline
Newbie
 
Join Date: Dec 2007
Posts: 8
Rep Power: 0
satya5321 is on a distinguished road
Default

you have to initialize the local variables if you want to use.

You maynot initialize the instance variables.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-23-2007, 05:03 PM
screamest screamest is offline
Newbie
 
Join Date: Dec 2007
Posts: 1
Rep Power: 0
screamest is on a distinguished road
Default

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;
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 01:25 AM.

Contest Stats

Xav ........ 162.68
Sacback ........ 120.05
alearb8 ........ 120.05
tfusion ........ 120
amr2107 ........ 120
d3s!gn ........ 120
Qoolman21 ........ 120
Pillager ........ 108
donfrench ........ 100.05
mc2o0o ........ 100

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 68%

Ads