Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-02-2007, 03:01 PM
Member
 
Join Date: Jul 2007
Posts: 36
boy22 is on a distinguished road
problem with a simple java code
I'm doing a simple java program, when I try to compile it, the eclipse says that age isn't initialized
this is my code, please check it out
Code:
public class First { public void calculate() { int age; if(age!= 0) { System.out.println(age) } else { System.out.println("you can't enter 0 as age") } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-02-2007, 03:30 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
yep the line where you have:
Code:
int age;
This is simply showing the compiler that you will using a variable called 'age' and it is of type int but not what the actual age is you need to modify this line to something like:
Code:
int age = 29;
This decalres it (as above) and then the = 29 bit sets age to be 29, obviously you can change this to any integer value you like.

Hope this helps
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-03-2007, 03:46 AM
Member
 
Join Date: Aug 2007
Posts: 15
yiweiang is on a distinguished road
Hi,

Shane Preater's method will solve this problem. But the importance of this issue is... variables declared inside of your methods ALWAYS need to be initialized before use. Otherwise a compiler error will result. However, that cannot be said for class variables. Those do not have to be declared before use.

Yi Wei
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

vB 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
a simple code (cldc or midp) to write a text file ouside of the application package sina CLDC and MIDP 2 03-13-2008 11:33 AM
problem while running java code.. Jjava New To Java 0 02-08-2008 07:33 AM
Peculiarty in code of simple program... Kreuz14 New To Java 4 01-23-2008 04:27 AM
simple problem - code wont compile dirtycash New To Java 1 11-20-2007 06:49 PM
simple code elizabeth New To Java 1 08-07-2007 07:49 PM


All times are GMT +3. The time now is 10:50 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org