Two Errors

I'm a beginner obviously and doing silly little programs to help me with some of the basics, but for some reason beyond my ken this one won't compile. The two errors I've documented on lines 27 and 31. Everything else seems fine. I would really like to know what I'm doing wrong. Thanks!

public class Starship

{

private int shieldStrength;

public Starship()

{

shieldStrength = 100;

}

void takeHit()

{

shieldStrength = shieldStrength - 25;

if (shieldStrength < 0) {

System.out.println("Direct hit! You have been destroyed!");

}else if (shieldStrength == 0) {

System.out.println("Direct hit! Shields down! We can't take another hit!");

}else {

System.out.println("Direct hit! Shields down to " + shieldStrength + "%!");

}

}

public static void main(String[] args)

{

Starship Voyager = new Starship();

}

while(shieldStrength >= 0) // illegal start of type - line 27

{

Voyager.takeHit();

}

} // <identifier> expected - line 31

[1107 byte] By [rrz4a] at [2007-11-26 21:09:02]
# 1
Carefully count your curly brackets; that while () statement is not in amethod body; the compiler expects the definition of another methodor member variable at such a location and it tried to tell you so.kind regards,Jos
JosAHa at 2007-7-10 2:44:46 > top of Java-index,Java Essentials,New To Java...
# 2

I appreciate you taking time to answer. Here's what I came up with, which seems to do what I wanted. Is this the direction I should have gone in?

--

public class Starship

{

private int shieldStrength; // instance variable

public Starship() // constructor

{

shieldStrength = 100;

}

void takeHit() // method for taking hit

{

while(shieldStrength >= 0)

{

shieldStrength = shieldStrength - 25;

if (shieldStrength < 0) {

System.out.println("Direct hit! You have been destroyed!");

}else if (shieldStrength == 0) {

System.out.println("Direct hit! Shields down! We can't take another hit!");

}else {

System.out.println("Direct hit! Shields down to " + shieldStrength + "%!");

}

}

}

public static void main(String[] args)

{

Starship Voyager = new Starship();

Voyager.takeHit();

}

}

--

But flow of execution in Java is something I still don't know much about. In this program execution starts with the Voyager constructor, then goes to the takeHit method, which loops several times before terminating. After that I'm assuming execution returns to the next line in the main, which is blank, and so the program ends.

But what if there were more. In an actual Java program (and not some contrived example such as mine) is there ever any executable code that isn't in a method?

Or is flow of execution basically just jumping from method to method to method as long as the program runs?

rrz4a at 2007-7-10 2:44:46 > top of Java-index,Java Essentials,New To Java...
# 3

Please use the code tags when typing up code

public class Starship

{

private int shieldStrength; // instance variable

public Starship() // constructor

{

shieldStrength = 100;

}

void takeHit() // method for taking hit

{

while(shieldStrength >= 0)

{

shieldStrength = shieldStrength - 25;

if (shieldStrength < 0) {

System.out.println("Direct hit! You have been destroyed!");

}else if (shieldStrength == 0) {

System.out.println("Direct hit! Shields down! We can't take another hit!");

}else {

System.out.println("Direct hit! Shields down to " + shieldStrength + "%!");

}

}

}

public static void main(String[] args)

{

Starship Voyager = new Starship();

Voyager.takeHit();

}

}

C_Zhaoa at 2007-7-10 2:44:47 > top of Java-index,Java Essentials,New To Java...
# 4

I believe the problem is the fact that the method "main" is inside the Starship class.

Try taking the "main" method out of the Starship class:

public class Test {

class Starship {

private int shieldStrength; // instance variable

public Starship() {

shieldStrength = 100;

}

void takeHit() {

while( shieldStrength >= 0 ) {

shieldStrength -= 25;

if ( shieldStrength < 0 )

System.out.println("Direct hit! You have been destroyed!");

else if ( shieldStrength == 0 )

System.out.println("Direct hit! Shields down! We can't take another hit!");

else

System.out.println("Direct hit! Shields down to " + shieldStrength + "%!");

}

}

}

public static void main( String[] args ) {

Starship Voyager = new Starship();

Voyager.takeHit();

}

}

I cleaned up the code a bit for you. As you can see, the class Test contains the Starship class and the method "main". I think you just misunderstood where to put the "main" method.

Oh, and on a side note, indenting is a very good idea when coding, makes everything nice and easy to read and debug.

C_Zhaoa at 2007-7-10 2:44:47 > top of Java-index,Java Essentials,New To Java...
# 5

Sorry about the formatting. I do indent, but I had no idea about the tags you mentioned. I checked the formatting tips, and it looks like standard HTML so I'll do that from now on. Thanks.

You're right that I wasn't clued into where the main goes. Putting it in the driver class separate from the object classes makes sense. But one thing still puzzles me. When I changed the file name and ran the code you showed me it gave this message:

non-static variable this cannot be referenced from a static context

in regards to this line:

Starship Voyager = new Starship();

Any idea why it's doing that?

rrz4a at 2007-7-10 2:44:47 > top of Java-index,Java Essentials,New To Java...