Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
Restrictions on the use of Fields during Initialization
Hemant Agarwal
Ranch Hand
Posts: 138
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
compile-time error results from the
test
program:
class Test { int i = j;// compile-time error: incorrect forward reference int j = 1; }
whereas the following example compiles without error:
class Test { Test() { k = 2; } int j = 1; int i = j; int k; }
Why is it so?
Stuart Ash
Ranch Hand
Posts: 637
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 135
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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).
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Hurting My Brains
Initializers...
for loop question
member assignmnet
static initialization blocks are there in exam?
More...