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 05-22-2008, 05:32 PM
Member
 
Join Date: May 2008
Posts: 1
rqal.10 is on a distinguished road
variable initialization
Following code giving error:
class A{
int a;
a=1;
}
but following code working fine:
class A{
int a=1;
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-22-2008, 08:14 PM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
What error is java giving you?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-22-2008, 11:10 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-24-2008, 07:21 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, according to the code segment you have to use non-static initialization block as follows.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-14-2008, 03:30 AM
Member
 
Join Date: Jun 2008
Posts: 22
ferranb is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-15-2008, 07:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by ferranb View Post
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.
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
Initialization and re-assignment of arrays Java Tip java.lang 0 04-14-2008 09:38 PM
initialization value problem ravian New To Java 2 01-28-2008 11:54 AM
Icon initialization problem saz25 AWT / Swing 1 12-24-2007 11:37 PM
Lazy Initialization onegcr New To Java 1 08-14-2007 04:29 PM
log4j initialization arfatkhan Web Frameworks 3 08-10-2007 08:42 PM


All times are GMT +3. The time now is 08:12 AM.


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