problem with static initializers

Discussion in 'Java' started by j l, Feb 23, 2004.

  1. j l

    j l Guest

    I have spent a couple of hours attempting something and have been totally
    unsuccessful. I was wondering if I could get some assistance on a
    particular homework problem.

    Homewok question from the professor.

    Read JLS section 8.5 on static initializers.

    Write two programs which produce different output and where the only
    difference in the programs is in the order of declarations of static
    variables.
    j l, Feb 23, 2004
    #1
    1. Advertising

  2. j l wrote:
    > I have spent a couple of hours attempting something and have been
    > totally unsuccessful. I was wondering if I could get some assistance
    > on a particular homework problem.
    >
    > Homewok question from the professor.
    >
    > Read JLS section 8.5 on static initializers.
    >
    > Write two programs which produce different output and where the only
    > difference in the programs is in the order of declarations of static
    > variables.


    ....Is this a troll?

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Feb 23, 2004
    #2
    1. Advertising

  3. j l

    Guest

    On Mon, 23 Feb 2004 12:04:24 GMT, j l <> wrote:

    >I have spent a couple of hours attempting something and have been totally
    >unsuccessful. I was wondering if I could get some assistance on a
    >particular homework problem.
    >
    >Homewok question from the professor.
    >
    >Read JLS section 8.5 on static initializers.
    >
    >Write two programs which produce different output and where the only
    >difference in the programs is in the order of declarations of static
    >variables.


    Perhaps if you showed us one of your attempts, assistance might be
    more forthcoming. This will usually be the case when dealing with
    homework in this newsgroup.

    Pete
    , Feb 23, 2004
    #3
  4. j l wrote:

    > I have spent a couple of hours attempting something and have been totally
    > unsuccessful. I was wondering if I could get some assistance on a
    > particular homework problem.
    >
    > Homewok question from the professor.
    >
    > Read JLS section 8.5 on static initializers.
    >
    > Write two programs which produce different output and where the only
    > difference in the programs is in the order of declarations of static
    > variables.


    Please note that it is section 8.7 (not 8.5) of the JLS, 2nd Edition,
    that discusses static initializers. Read the section, and pay attention
    to what it says about the order of declarations. As your instructor has
    pointed you to material about static initializers, it is probably safe
    to assume that he or she envisions a solution that makes use of one or
    more of them.

    If you don't understand what you have read then post a specific question
    here, or (perhaps better) in comp.lang.java.help. If you have code that
    you think should solve the problem but doesn't, then you could post it
    here or in c.l.j.h and ask for advice.


    John Bollinger
    John C. Bollinger, Feb 23, 2004
    #4
  5. j l

    j l Guest

    Thanks for the replies,

    And no, I am not a troll. I do not hang in this newsgroup and am curious
    why I would be thought a troll? What in the question would make someone
    think that?

    My instructor, who is never wrong by the way, specified 8.5. I too have
    concluded that it is 8.7. He will jump up and down and scream when you
    make a mistake, but he is allowed to make any mistake, but that is
    another story...

    I have scoured the web and came up with what appears to be a solution.
    The code follows. Do the readers of this post think that this is an
    acceptable answer to the question posed?

    When class BS1 runs, output is: 5
    When class BS2 runs, output is: 4

    class A {
    public static int A = 1 + B.B;
    }


    class B{
    public static int B = 2 + A.A;
    }


    public class BS1 {
    public static void main (String [] args){
    int a = A.A;
    int b = B.B;
    System.out.println (a + b);
    }


    public class BS2 {
    public static void main (String [] args){
    int b = B.B;
    int a = A.A;
    System.out.println (a + b);
    }

    In article <c1df3i$vfe$>,
    says...
    > j l wrote:
    >
    > > I have spent a couple of hours attempting something and have been totally
    > > unsuccessful. I was wondering if I could get some assistance on a
    > > particular homework problem.
    > >
    > > Homewok question from the professor.
    > >
    > > Read JLS section 8.5 on static initializers.
    > >
    > > Write two programs which produce different output and where the only
    > > difference in the programs is in the order of declarations of static
    > > variables.

    >
    > Please note that it is section 8.7 (not 8.5) of the JLS, 2nd Edition,
    > that discusses static initializers. Read the section, and pay attention
    > to what it says about the order of declarations. As your instructor has
    > pointed you to material about static initializers, it is probably safe
    > to assume that he or she envisions a solution that makes use of one or
    > more of them.
    >
    > If you don't understand what you have read then post a specific question
    > here, or (perhaps better) in comp.lang.java.help. If you have code that
    > you think should solve the problem but doesn't, then you could post it
    > here or in c.l.j.h and ask for advice.
    >
    >
    > John Bollinger
    >
    >
    >
    j l, Feb 23, 2004
    #5
  6. In article <>, j l wrote:
    > Thanks for the replies,
    >
    > And no, I am not a troll. I do not hang in this newsgroup and am curious
    > why I would be thought a troll? What in the question would make someone
    > think that?


    Not a troll, but someone looking to have his homework done by someone else.

    > My instructor, who is never wrong by the way, specified 8.5. I too have
    > concluded that it is 8.7. He will jump up and down and scream when you
    > make a mistake, but he is allowed to make any mistake, but that is
    > another story...


    The section on Static Initializers is section 8.5 of the first edition
    of the Java Language Specification; it is section 8.7 of the second
    edition.

    > I have scoured the web and came up with what appears to be a solution.
    > The code follows. Do the readers of this post think that this is an
    > acceptable answer to the question posed?

    [solution snipped]

    Whether it is acceptable depends on how strictly your instructor
    intreprets the assignment, which was "Write two programs which produce
    different output and where the only difference in the programs is in
    the order of declarations of static variables."

    The solution from the web is a single program, not two programs. The
    single program does not have different orders of declarations of
    static variables. It has different orders of static initialization of
    static variables.
    A. Bolmarcich, Feb 23, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Robert Leek

    JNI Invocation: static UN-initializers?

    James Robert Leek, Aug 31, 2004, in forum: Java
    Replies:
    2
    Views:
    479
    James Robert Leek
    Aug 31, 2004
  2. HK
    Replies:
    7
    Views:
    408
    Wibble
    May 30, 2005
  3. HK
    Replies:
    3
    Views:
    4,156
  4. Ric Has
    Replies:
    3
    Views:
    3,229
    Ric Has
    Oct 13, 2004
  5. Christopher Benson-Manica

    References to static fields in enum initializers

    Christopher Benson-Manica, Jul 5, 2007, in forum: Java
    Replies:
    1
    Views:
    333
    Christopher Benson-Manica
    Jul 5, 2007
Loading...

Share This Page