problem with static initializers

J

j l

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.
 
A

Andrew Thompson

j said:
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?
 
G

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.

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
 
J

John C. Bollinger

j said:
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 address removed)
 
J

j l

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);
}
 
A

A. Bolmarcich

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top