Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > problem with static initializers

Reply
Thread Tools

problem with static initializers

 
 
j l
Guest
Posts: n/a
 
      02-23-2004
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.
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      02-23-2004
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


 
Reply With Quote
 
 
 
 
nospam@tampabay.rr.com
Guest
Posts: n/a
 
      02-23-2004
On Mon, 23 Feb 2004 12:04:24 GMT, j l <(E-Mail Removed)> 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
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      02-23-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
j l
Guest
Posts: n/a
 
      02-23-2004
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$(E-Mail Removed)>, (E-Mail Removed)
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
> (E-Mail Removed)
>
>



 
Reply With Quote
 
A. Bolmarcich
Guest
Posts: n/a
 
      02-23-2004
In article <(E-Mail Removed) et>, 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
References to static fields in enum initializers Christopher Benson-Manica Java 1 07-05-2007 06:41 PM
exceptions in static initializers HK Java 3 05-31-2005 11:50 AM
static initializers vs. cached values HK Java 7 05-30-2005 05:28 PM
odd problem with static initializers Ric Has C++ 3 10-13-2004 06:12 PM
JNI Invocation: static UN-initializers? James Robert Leek Java 2 08-31-2004 09:13 PM



Advertisments