|
|
|
|
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.
|
|

05-22-2008, 05:32 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 1
|
|
|
variable initialization
Following code giving error:
class A{
int a;
a=1;
}
but following code working fine:
class A{
int a=1;
}
|
|

05-22-2008, 08:14 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 23
|
|
|
What error is java giving you?
|
|

05-22-2008, 11:10 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Canada
Posts: 183
|
|
Come on  You cannot do a=1; in the class body. You have to put that in a method, static bloc or initialize the variable on the same line where you declare it like in second example.
__________________
Daniel @ [ To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. ]
Language is froth on the surface of thought
|
|

05-24-2008, 07:21 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Yes, according to the code segment you have to use non-static initialization block as follows.
class A {
int a;
{
a = 1;
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-14-2008, 03:30 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 22
|
|
|
In the first example, "a=1" is an assigment sentence, and assigment sentences only are allowed inside methods.
The second one is an initializer declaration and can be on classes attributes or local variables.
Ferran
|
|

06-15-2008, 07:39 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Originally Posted by ferranb
In the first example, "a=1" is an assigment sentence, and assigment sentences only are allowed inside methods.
Inside installation blocks too. Normally we don't call them as methods. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
All times are GMT +3. The time now is 12:36 AM.
|
|
VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org