aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question regarding initialzer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question regarding initialzer." Watch "question regarding initialzer." New topic
Author

question regarding initialzer.

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
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

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You may find that this earlier thread helps you.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
navneet shrivastava
Ranch Hand

Joined: Jul 09, 2004
Posts: 39
interesting question.
i guess for this we need to know how JVM/compiler behaves.
waiting for experts excerpts.

navneet


+nav
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175

(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 ]

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 10, 2004
Posts: 175
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

Joined: Aug 31, 2004
Posts: 11343

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 ]
PNS Subramanian
Ranch Hand

Joined: Jul 13, 2004
Posts: 150
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 ?
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
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.
Nandish KB
Greenhorn

Joined: Aug 18, 2004
Posts: 6
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


Regards,<br />Nandish
Nandish KB
Greenhorn

Joined: Aug 18, 2004
Posts: 6
Ohhhhhhhhhh sorrrrrrrry,
I actually written those codes in main() method.
Sorry to post wrong Qn ! :-(
Regards,
Nandish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question regarding initialzer.