This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Restrictions on the use of Fields during Initialization
 
Similar Threads
static initialization blocks are there in exam?
for loop question
Initializers...
Hurting My Brains
member assignmnet