• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

question regarding initialzer.

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Looking at the code below, I don't understand why the statement at (5) is valid since variable 'height' has not been declared before it is initialized?
could somebody help me out? Thanks

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find that this earlier thread helps you.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interesting question.
i guess for this we need to know how JVM/compiler behaves.
waiting for experts excerpts.

navneet
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I do get an illegal forward reference error with the above code, as I do with the following...

(I'm running 1.3 at work. Might that be the difference?)
[ September 20, 2004: Message edited by: marc weber ]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(I'm running 1.3 at work. Might that be the difference?)

[ September 20, 2004: Message edited by: marc weber ][/qb]<hr></blockquote>



exact same code works for me it should
here is an other good example from the java language spec

[B][/B]
web page

[ September 20, 2004: Message edited by: Inuka Vincit ]
[ September 20, 2004: Message edited by: Inuka Vincit ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Inuka Vincit:
exact same code works for me it should


Interesting... I'll have to check this using 1.4 when I get home.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update: Both pieces of code work with 1.4, but neither worked with 1.3.

Note: The UseBeforeDeclaration class above is from the Second Edition of the JLS (section 8.3.2.3). I couldn't find anything to parallel it in the First Edition.

JLS, 2nd Ed., Sect. 8.3.2.3: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406
[ September 20, 2004: Message edited by: marc weber ]
 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
Update: Both pieces of code work with 1.4, but neither worked with 1.3.

Note: The UseBeforeDeclaration class above is from the Second Edition of the JLS (section 8.3.2.3). I couldn't find anything to parallel it in the First Edition.

JLS, 2nd Ed., Sect. 8.3.2.3: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406

[ September 20, 2004: Message edited by: marc weber ]



baah hate when that sort of thing happened, end up confusing your self

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I definitely learned something. Thanks for the example!

What's instructive to me is the way they phrase the comments in the JLS code: "//error - read before declaration." Technically, I suppose it's all the same, but the problem of trying to "read" a variable before it's declared makes more sense to me than discerning "left/right" in something like "int a = b = 20."
[ September 21, 2004: Message edited by: marc weber ]
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the message i get for the code below
C:\Data\bea8.1\jdk141_03\bin\Test2.java:6: cannot resolve symbol
symbol : variable j
location: class Test2
int i = j = 20;
^
1 error

Process completed with exit code 1




Does this have anything to do with the version being used ?
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you overlooked declaring j. The code you posted doesn't contain declaration for j hence the error.

declare the variable j before you use. Since j is local variable you cannot have forward referencing.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey I checked the same code with Eclipse.Its giving compiler error.
Even I tried with

int a=b=3;
static int b; // this also gives me error, I think it should work rt ?. as forward referncing for static vars is allowed


int a = b = 4;
int b; // here also its giving errr

plz clarify my doubt

Thanx
Nandish
 
Nandish KB
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhhhhhhhhh sorrrrrrrry,
I actually written those codes in main() method.
Sorry to post wrong Qn ! :-(
Regards,
Nandish
reply
    Bookmark Topic Watch Topic
  • New Topic