File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Restrictions on the use of Fields during Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Restrictions on the use of Fields during Initialization" Watch "Restrictions on the use of Fields during Initialization" New topic
Author

Restrictions on the use of Fields during Initialization

Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
compile-time error results from the test program:



whereas the following example compiles without error:




Why is it so?
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Because methods, including constructors, are "after" fields: they are considered separately from the field declarations.
But between two fields, order matters.


ASCII silly question, Get a silly ANSI.
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
To put it another way: Your first assignment refers to variable j, which hasn't been declared yet, so the compiler doesn't recognize it (j).
 
jQuery in Action, 2nd edition
 
subject: Restrictions on the use of Fields during Initialization