Forward Reference

Hi all,
I seem to have a problem in understanding the Java Specs relative to Forward Reference during initialization.
It goes like this:
"8.3.2.3 Restrictions on the use of Fields during Initialization
The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
The usage is not on the left hand side of an assignment.
C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the three requirements above are not met."
you can find it at http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406
A worthy example follows:
class UseBeforeDeclaration {
     static {
          x = 100; // ok - assignment
          int y = x + 1; // error - read before declaration
          int v = x = 3; // ok - x at left hand side of assignment
          int z = UseBeforeDeclaration.x * 2;
     // ok - not accessed via simple name
          Object o = new Object(){
               void foo(){x++;} // ok - occurs in a different class
               {x++;} // ok - occurs in a different class
          j = 200; // ok - assignment
          j = j + 1; // error - right hand side reads before declaration
          int k = j = j + 1;
          int n = j = 300; // ok - j at left hand side of assignment
          int h = j++; // error - read before declaration
          int l = this.j * 3; // ok - not accessed via simple name
          Object o = new Object(){
               void foo(){j++;} // ok - occurs in a different class
               { j = j + 1;} // ok - occurs in a different class
     int w = x= 3; // ok - x at left hand side of assignment
     int p = x; // ok - instance initializers may access static fields
     static int u = (new Object(){int bar(){return x;}}).bar();
     // ok - occurs in a different class
     static int x;
     int m = j = 4; // ok - j at left hand side of assignment
     int o = (new Object(){int bar(){return j;}}).bar();
     // ok - occurs in a different class
     int j;
}My compiler complains where it should not, for example at the very first assignment x = 100; // ok - assignment . Furthermore, how do you account for the following line:
int z = UseBeforeDeclaration.x * 2;
// ok - not accessed via simple name, I just don't understand why this works while the unqualified counterpart int z = x * 2 does not seem to work. What is the difference? According to the requirements none of the code in first static block should work.
If anyone could shed some light with more examples or even a detailed explaination line by line of the above class, I would be very grateful.
Thank you
luca

My compiler complains where it should not, for example
at the very first assignment
����x = 100; // ok - assignment If you get a compiler error at that line, then your compiler has a bug in it. What compiler are you using?
Furthermore, how do you account
for the following line:
����int z = UseBeforeDeclaration.x * 2; // ok - not accessed via simple name
, I just don't understand why this works while the
unqualified counterpart
����int z = x * 2
does not seem to work. What is the difference?I can see your confusion here. The JLS appears only to say that this is okay in the example, but not in any text. I too would be interested in any explainatino that could be offered for this.
According to the requirements none of the code in
first static block should work.I don't agree. The only code in the first static initialiser block that should fail is the line
����int y = x + 1; // error - read before declaration

Similar Messages

  • Dbx forward reference not defined (for templated variable)

    I get the 'forward reference not defined' message very frequently when working with the Forte 6.1 C++ compiler and workshop. On the one occasion the variable wasn't an instance of a templated class, doing module <file>.cc fixed the problem. But what do I do for templated variables? Issuing the module command on the header file where they are defined doesn't work. It's very annoying, because it means that most of the time I can't inspect templated variables in the debugger.
    Regards,
    Martin.

    Haven't seen this myself but I saw this after some searching.
    http://forum.brightcove.com/t5/Player-APIs/new-error-on-ContextMenuClipboardItems/td-p/228 6
    Someone mentioned in the thread that compiling for FP10 as a workaround, can you try that.

  • JavaFX: Specifying explicit type causes Illegal Forward Reference

    I can't seem to find a way to work around this, but if I specify some code like:
    var txtField = SwingTextField {
        columns: 3
        text: "60"
        editable: true
        action: function() {
            someMethod();
    var sceneMain:Scene = Scene { content: [
        txtField
    var stage:Stage = Stage {
        title: "My App"
        width: 300
        height: 300
        scene: sceneMain
    }I get an error on the declaration of txtField, saying I seed to specify an explicit type. OK, no problem, change the code to:
    var txtField:SwingTextField = SwingTextField {However, now the compiler reports a warning on the line:
    var sceneMain:Scene = Scene { content: [
        txtField
    ]}saying that there is an illegal forward reference to "txtField" here.
    The really odd part is that this "illegal forward ref" only happens if txtField is defined with an explicit type. If I remove the need for an explicit type declaration by removing the action, the "illegal forward ref" warning goes away:
    var txtField = SwingTextField {
        columns: 3
        text: "60"
        editable: true
        // action: function() {
        //    someMethod();
    var sceneMain:Scene = Scene { content: [
        txtField
    var stage:Stage = Stage {
        title: "My App"
        width: 300
        height: 300
        scene: sceneMain
    }I can't seem to find a way around this one... is this a JFX bug? Thanks for any help!
    Edited by: rally25rs on Apr 1, 2009 8:04 PM
    Fixed code markup

    Well, I guess I figured out my "error", though I'd argue its a problem with the way JavaFX loads its classes.
    I can add 1 line to reference the button earlier and force it to load. Its a completely useless line, but for whatever reason gives the compiler enough of a hint to fix the error:
    package testapp;
    import javafx.stage.*;
    import javafx.scene.*;
    import javafx.ext.swing.*;
    function setBpmFromTxt():Void {
        // validate some user input.
        // if a problem, show error scene:
        cmdStartStop;  // <-- Adding this fixes error. Does nothing at runtime, but tips off the compiler. Awesome!
        stage.scene = sceneError;
    var cmdStartStop:SwingButton = SwingButton {
        action: function() {
                setBpmFromTxt();
    var sceneError:Scene = Scene { content: [
    var sceneMain:Scene = Scene { content: [
        cmdStartStop
    var stage:Stage = Stage {
        scene: sceneMain
    }

  • Illegal forward reference

    hi...i'm new to socket programming and i encountered this error: "illegal forward reference" when i tried to compile code with this single line:
    SocketChannel socketChannel = socketChannel.open(new InetSocketAddress(5000));
    why is this occuring? i would really appreciate it if someone could help me out...thank you.

    http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/SocketChannel.html

  • LDAP - eDir - Forward reference - Unknown objectclass

    Hello,
    I have the following question:
    When loading a LDIF file and when "Forward Reference" is checked while
    importing the LDIF file, attributes which contains a DN like cn=a,o=b will
    automatic create an object from the objectclass "Unknown" when the object
    does not exist in eDir. When later on the object is found in the LDIF file,
    the object is changed and updated to its proper objectclass and properties.
    If the object is not found in the ldif file the created object stays from
    the type objectclass "Unknown" until later another LDIF file was loaded
    which contains the object.
    So far so good. But I need to do this manualy.
    I want to create is the following application which is loading objects from
    "a source", one by one and create them in eDir.
    If one of those objects contains an attribute with a DN like cn=a,o=b the
    object can not be created when the refering object does not exist in eDir.
    Although I can create an object of the objectclass "Unknown" before
    creating the object.
    This makes the creation of the object possible.
    But can I later overwrite the created object from the objectclass "Unknown"
    with its proper properties when the object is discovered from the source?
    So what I mean is, can I alter the objectclass attribute and fill in the
    proper attributes?
    Untill now I had no success or did not find any examples.

    Hello,
    I have the following question:
    When loading a LDIF file and when "Forward Reference" is checked while
    importing the LDIF file, attributes which contains a DN like cn=a,o=b will
    automatic create an object from the objectclass "Unknown" when the object
    does not exist in eDir. When later on the object is found in the LDIF file,
    the object is changed and updated to its proper objectclass and properties.
    If the object is not found in the ldif file the created object stays from
    the type objectclass "Unknown" until later another LDIF file was loaded
    which contains the object.
    So far so good. But I need to do this manualy.
    I want to create is the following application which is loading objects from
    "a source", one by one and create them in eDir.
    If one of those objects contains an attribute with a DN like cn=a,o=b the
    object can not be created when the refering object does not exist in eDir.
    Although I can create an object of the objectclass "Unknown" before
    creating the object.
    This makes the creation of the object possible.
    But can I later overwrite the created object from the objectclass "Unknown"
    with its proper properties when the object is discovered from the source?
    So what I mean is, can I alter the objectclass attribute and fill in the
    proper attributes?
    Untill now I had no success or did not find any examples.

  • Problem with forward reference

    Java requires that the declaration of a field must occur before its usage in any initializer expression, if the field is used on the right-hand side of an assignment in the initializer expression
    class NonStaticInitializers {
         int sqSide = height = 20;         
        int height;                        
    }'height' is used first and declared later how the above initialization is permitted?

    I guess that class attributes are first alldeclared,
    then initialized...
    should be somewhere in the JLS...see section 8.3.2 of the JLS... I didn't bother to
    read it though... it's friday afternoon, dammit ! ^_^... and I'm bored...
    The declaration of a member needs to appear before it is used only if the
    member is an instance (respectively static) field of a class or interface C and all
    of the following conditions hold:
    � The usage occurs in an instance (respectively static) variable initializer of C
    or in an instance (respectively static) initializer of C.
    � The usage is not on the left hand side of an assignment.
    � C is the innermost class or interface enclosing the usage.
    A compile-time error occurs if any of the three requirements above are not
    met.

  • Static Forward References

    hi everyone
    i have the follwoing program, and the output was:
    sf1: 50
    sf2:30
    my question : why sf2 in particular was 30, i thought that would be 20
    class StaticForwardReferences {
        static {           
            sf1 = 10;
            int b = sf1 = 20;
            int c = StaticForwardReferences.sf1;
        static int sf1 = sf2 = 30; 
        static int sf2;            
        int if1 = 5;                //  Non-static field
        static {             
            int d = 2 * sf1; 
            int e = sf1 = 50;
        public static void main(String[] args) {
            System.out.println("sf1: " + StaticForwardReferences.sf1);
            System.out.println("sf2: " + StaticForwardReferences.sf2);
    }thunx for anyhelp

    but she has a good point.OOp....plz. change (she) to (he), and such this conflict must be no matther, no problem.
    Code should be easy to read. It's not about "who can decipher the
    most complicated code".this code from a java book, not from my head, the author is Khaled Mughal,
    http://www.ii.uib.no/%7Ekhalid/pgjc2e/source.html
    I read the explanation of the author twice, but I don't beleive his thinkness, SO I ASKED.
    thunx for any help, if no help, no problem
    about your question:
    static int sf1 = sf2 = 30; Why should it be 20?because sf1 is modified to 20 in the FIRST static block
    about your commet:
    Sure. Avoid using constructs people, including yourself, are likely to >understand only after an extensive study of the Java Language >Specification. Just because you can do something doesn't mean you >should to do it, especially not if you can hardly understand it yourselfwhat is this? do u have another way to talk?
    I suggest that you learn how to speak instead of how to learn java

  • Cross reference or hyperlinking not working for InDesign CS5 Book to ePub

    I cannot get hyperlinks or cross reference links to work when generating an ePub from InDesign CS5. (7.0.0.355, Mac OS 10.6.2)
    Here's how to recreate a simple test demonstrating the issue.
    1. Create a new InDesign Book (File -> New -> Book)
    2. Create document one (File -> New -> Document)
    3. Create a text box and type "Table of Contents"
    4. Create document two (File -> New -> Document)
    5. Create a text box in it and type "Story One"
    6. Create a new paragraph style and assign it to the Story One text
    7. Save document 2. I just kept the name Untitled-2.xhtml
    8. Back in document 1, add a cross-reference and select document 2, the style you created, and then "Story One". "Story One" should now appear under the previously typed Table of Contents text, and when highlighed, should show it is an active cross reference in the Hyperlinks panel.
    9. Save document 1. I just kept the name Untitled-1.xhtml
    10. Add both documents to the Book and save the book.
    11. From the Book panel, select Export Book to EPUB...
    12. Save the epub file and open it in Adobe Digital Editions.
    Note that the text "Story One" appears as a hyperlink but when clicked does not switch to the document 2. It does nothing apparent when clicked.
    Looking at the XHTML source for the first document, it is immediately evident why. The filename for document 2.thml is missing.  It looks like this: <a href="#anchor-anchor">
    When it should be : <a href="Untitled-2.xhtml#anchor-anchor">
    The same thing works if you create a hyperlink instead of a cross reference.
    I note then when I use the option to go to the destinate reference in the Cross Link (hyperlink) panel, it does switch to the second InDesign docuemtn. So the cross reference is functioning (in InDesign). But the link is broken in the ePub for the above mentioned reason.
    Bug? Something I'm overlooking?
    Entire source of Untitled-1.xhtml:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled-1.xhtml</title>
    <link href="template.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <div id="untitled-1">
    <div class="generated-style">
    <p class="basic-paragraph" xml:lang='en-us'><span class="no-style-override">Table of Contents</span></p>
    <p class="basic-paragraph" xml:lang='en-us'><span class="no-style-override"><a href="#anchor-anchor">“story one”</a></span></p>
    </div>
    </div>
    </body>
    </html>

    As far as I can tell, it has NOT yet been fixed in CS5.5.  I'm having the same problem.  I have about 90 hyperlinks to fix.
    And the explanation that Teus Dejong gives indicates it's not going to be that helpful:  "we have found that only the links in documents split by the CS5 exporter will be fixed. For books the script does only work for references to earlier documents in the book, not for forward references. This means in practice that for most books the script will not repair the links and should be seen as a means to add the year only. At the moment I see no way to repair this, because the book epub output of InDesign does not contain sufficient information to repair links referencing forward to others documents in a book."
    It's pretty discouraging.

  • PLS-00323 forward declaration in PLSQL Version 10.

    Hello.
    I have a package which contains forward references which compiles on a 9i database.
    However, when I try to compile an exact copy of the procedure on a 10g database it returns an error
    "PLS-00323: subprogram or cursor 'P_PA_ACTION_START' is declared in a package specification and must be defined in the package body"
    I was wondering if there were any differences in the rules concerning forward references in pl/sql 10.
    Thanks in advance.
    Glyn Williams.

    Hello.
    I have a package which contains forward references which compiles on a 9i database.
    However, when I try to compile an exact copy of the procedure on a 10g database it returns an error
    "PLS-00323: subprogram or cursor 'P_PA_ACTION_START' is declared in a package specification and must be defined in the package body"
    I was wondering if there were any differences in the rules concerning forward references in pl/sql 10.
    Thanks in advance.
    Glyn Williams.

  • Rationalization of Cross Reference or XRef

    Hi Friends,
                      I would like to know what does rationalization of cross references mean and how to do the rationalization of Cross Refeence? Please be very detail. I will assign highest points for the person who can provide me a detailed answer.
    I would appreciate greatly, if i can get the best answer.
    Thanks
    Venkata

    As far as I can tell, it has NOT yet been fixed in CS5.5.  I'm having the same problem.  I have about 90 hyperlinks to fix.
    And the explanation that Teus Dejong gives indicates it's not going to be that helpful:  "we have found that only the links in documents split by the CS5 exporter will be fixed. For books the script does only work for references to earlier documents in the book, not for forward references. This means in practice that for most books the script will not repair the links and should be seen as a means to add the year only. At the moment I see no way to repair this, because the book epub output of InDesign does not contain sufficient information to repair links referencing forward to others documents in a book."
    It's pretty discouraging.

  • Can a BIG form be served up one page at a time to avoid long load time?

    Tricks I have read for optimizing the load time of large forms are not helping. Linearization causes the first page to render quickly, but you can't interact with the fields until the whole form finishes loading -- no help there. Is there a way to break the form into pages (without creating entirely separate forms) so the user can fill out a page, hit a Next Page button, fill out that page, etc.? Understood that this is an old school idea, but until Reader can download a 1+ MB form in less time than it takes an average user to get ticked off, old school might do the trick.
    Alternatively, is there a way to construct a form so you can start interacting with it without having to wait for it all to load? This question comes from the (uninformed) assumption that maybe there are forward references that can't be satisfied until all the bits have come over the wire. If that's right, can a multipage form be architected so as to avoid this problem?

    No that technology does not exist yet. There are form level events that need to have the entire document there before they can fire. Also you would have to keep track of where you are so that would mean some sort of session information for each user.

  • How the memory alloaced

    Hello friend i am new to java...
    Please help me out..
    class RaceCar
    private int carNumber;
    private static int count ;
    public RaceCar()
    count++;
    carNumber = count;
    public void showCarNumber()
    System.out.println("Car number is " + carNumber);
    public static void showCount()
    System.out.println("Count is " + count);
    class RaceCarDemo
    public static void main(String[] args)
    //block 1
    RaceCar x = new RaceCar();
    RaceCar y = new RaceCar();
    RaceCar a = new RaceCar();
    RaceCar d = new RaceCar();
    RaceCar dd = new RaceCar();
    RaceCar yd = new RaceCar();
         //which line is compiled first
         // Flow of execution
    //block 2
    x.showCarNumber();
    y.showCarNumber();
              //how is the car number of is x is show 1 and y as 2 correctly.
              //does it store the values in x,y,...
    //block 3
    RaceCar.showCount();
    d.showCount();
              //Can we call showCount with any object of the race car.
              // where the value of count is stored
    x.showCarNumber();
    Q Are all the Blocks 1 2 3 are compiled one after the other or all the variable of x, y ,... are compiled or after the other, i want to how exactly the program is compiles... like in c++ by pressing f7(may be f6) we can compile one after the other statement, in the same way do we have in java... so that i can check which statement compile one after the other.
    // Flow of execution
    //how is the showCarNumber of is x is show 1 and y as 2 correctly, where the value of x is stored.
    //does it store the values in x,y,...
    Thank you.
    Edited by: 882702 on Aug 31, 2011 2:47 PM

    First of all, you should format your code by placing it between \\ tags.
    >
    Q Are all the Blocks 1 2 3 are compiled one after the other or all the variable of x, y ,... are compiled or after the other, i want to how exactly the program is compiles
    >
    I am not sure I understand your question, but compilation order is pretty much irrelevant in Java. The compiler makes multiple runs through the code so it does not need any equivalent to the C++ forward reference.
    If you are asking about the order it will run in, the code should run in the order it is given.
    >
    //how is the showCarNumber of is x is show 1 and y as 2 correctly, where the value of x is stored.
    //does it store the values in x,y,...
    >
    Could you please rephrase these and explain in more detail?

  • Order of execution and variable access

    Hello frnds,
    This prog shows the order of its execution:
    public class Sequence {
    int j=10;
    public Sequence() {
    System.out.println(" constructor initializer : "+i);
    // line x
    System.out.println(" instance initializer :"+this.i);
    static
    System.out.println(" static initializer");
    public void method() {
    System.out.println(" method initializer");
    public static void main(String args[]) {
    Sequence sq = new Sequence();
    sq.method();
    int i=20;
    output is :
    static initializer
    instance initializer :0
    constructor initializer : 20
    method initializer
    If at // line x i edit it like this
    System.out.println(" instance initializer :"+i);
    it gives compile time exception :
    illegal forward reference
    System.out.println(" instance initializer :"+i);
    ^
    Why is to so with (this.i) it does not gives compile time error.

    Restrictions on the use of fields during instance initialization:
    Instance initializers are permitted to refer to the current object this, to any type variables in scope and to use the keyword super.
    But, use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope.
    Precisely, the declaration of a member needs to appear textually before it is used only if the member is an instance field of a class or interface C and all of the following conditions hold:
    - The usage occurs in an instance variable initializer of C or in an instance initializer of C.
    - The usage is not on the left hand side of an assignment.
    - The usage is via a simple name.
    - C is the innermost class or interface enclosing the usage.

  • Scriptlet  Variable Scope

    I have a JSP which has several scriptlet sections. Recently I ran into a problem where it appears I cannot reference a variable declared in say scriptlet1 in scriptlet2. My question is what is causing this behaviour and what might be some suggested work arounds?
    example:
    MyJsp.jsp
    Working code......
    <% int var1 = 0;
    var1 =1;
    %>
    <% int t1=0;
    t1 = var1----This causes a compile time error, variable not found in class, referring to var1.
    %>
    Working code........
    Thanks in advance for your replies.
    Wayne
    t1

    The variables should be visible as per JSP specifications. The only possibility I can think of is a forward reference where you try to access a variable even before it is defined.
    e.g:
    <%
    int i = j; //<= This will give an error since j is not yet defined
    %>
    <%
    int j = 10;
    // j is visible only from this point forward
    %>

  • Communication of Illustrator with Third Party Application (MFC).

    Hi all,
    I want to use illustrator's APIs into MFC application. How can I do this?
    I searched a lot and found that, it could happen using COM Interface of Illustrator Type Library.
    so I included Illustrator CS5 Type Library and import "ScriptingSupport.aip".
    When I build the project it will create a "scriptingsupport.tlh" which contains the Forward references and typedefs in Illustrator Namespace.
    I have created a window application in MFC containing a button.
    OnButtonClick I wanted to access Illustrator APIs but I am unable to do it. I have tried like this:
    Illustrator::_ApplicationPtr ptr;
    //some script.
    ptr->DoJavaScript(....);
    In this case I am not able to find out how to get instance of _ApplicationPtr in ptr variable.
    Please help me if anybody have any idea.
    Thanks in Advance...

    Which type of 3rd party application is this, and which SSO authentication methods does it support?
    If you can find a common one, then that will be good for you.
    Specifically for non-SAP systems re-using the SAP LogonTickets, I know that you can extract the user name from the ticket. I think SAP even provides some verification tools here for external applications to verify the ticket?
    Currently there is much excitement about SAML 2.0 which is also worth taking a look into as well.
    Cheers,
    Julius

Maybe you are looking for

  • ITunes deleted all my music from my iPod, but they still show up when I plug in the iPod.

    Hi! I had my iPhone 4S plugged into my computer, and I was listening to the music on my phone from the music library on my phone through iTunes. Well, after the phone got disconnected, I plugged it back in, and I was unable to play any of the songs o

  • Outlook 2013 Free/Busy can not retrieve information

    Currently have a problem with Outlook 2013 Free/Busy can not retrieve information. All windows updates are installed.  Exchange 2007 is being used.  Problem started last week.  Already Tried without any progress: 1) Recreated profile (Fixed the issue

  • Directshow9 and Quicktime 7 error

    I just upgraded to a 64-bit system, and am not experiencing problems importing videos into my presentations.  "A required system library did not install properly.  Please install directshow9 and quicktime 7 or higher..." error shows up; but I have bo

  • WSDL to ABAP in 620 versus WSDL to ABAP in 640

    Can I say that <b>If WAS 640:</b> Then its pretty staigtforward to convert the WSDL to ABAP via proxy object generation but <b>If WAS 620</b>: Then we need to write code in XSLT which will parse the WSDL to  build the SOAP message and then the output

  • Sun solaris server

    hi, i'm a mexican boy, and first i want to apologize me due to my writing, cause its a little difficult to me me to speak English. well this is my problem: as i mention i have a sun solaris server in wich i had installed ubuntu 7.04 (server) but duri