Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: unable to differentiate legal and illegal forward reference

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default unable to differentiate legal and illegal forward reference

    I understand that the following is an example of valid reference in java. I also understand that at class creation, first i and j are initialized to 0. j is then re-initialized to 5, as a result of which i's value is 0 and j's is 5 inside main.
    class J
    {
     static int test()
     {
      return j;
     }
     static int i = test();
     static int j=5;
     
     public static void main(String[] args)
     {
      System.out.println(i);
      System.out.println(j);
     }
    }

    But why is the same not true for the following ? Why is it that i and j are not initialized to the default value of 0 in this case and gives an illegal forward reference?
    class test
    {
     static int i = j;
     static int j=5;
     
     public static void main(String[] args)
     {
      System.out.println(i);
      System.out.println(j);
     }
    }

    Why is it legal forward reference when using a method, and illegal when pretty much the same thing is done using a variable ?

    Please advise.
    Thanks.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,377
    My Mood
    Mellow
    Thanks
    143
    Thanked 599 Times in 586 Posts

    Default Re: unable to differentiate legal and illegal forward reference

    Cross posted here.

    Welcome to the forum! Please read this topic for other useful info for new members, including the forum's preference for cross posts.

Similar Threads

  1. Looking forward to working with everyone.
    By Knowledge_Feeds_The_Mind in forum Member Introductions
    Replies: 1
    Last Post: February 27th, 2014, 01:22 AM
  2. Connecting to my torrent client (I use it for legal stuff only!)
    By jdruwe in forum Java Theory & Questions
    Replies: 0
    Last Post: August 9th, 2012, 10:16 AM
  3. Some legal questions about programming (intellectual property)
    By clydefrog in forum Totally Off Topic
    Replies: 13
    Last Post: April 6th, 2012, 11:08 AM
  4. Forward to a link from Java
    By makdu in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 30th, 2010, 05:45 AM
  5. Method for legal moves in connect four.
    By mandar9589 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 27th, 2009, 04:28 PM