• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restrictions on the use of Fields during Initialization

 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compile-time error results from the test program:



whereas the following example compiles without error:




Why is it so?
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because methods, including constructors, are "after" fields: they are considered separately from the field declarations.
But between two fields, order matters.
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic