What is wrong with my code?
I am a very new beginner, I mean I almost seriously started yesterday. I have the Java All In One For Dummies 9 Books In One , and in the first book, but I am confused. If there is anybody out there to provide regular help and clarification, please, but all means, contact me. But here is my first code, which still can't compile even though its word for word from the book.
public class HelloApp
{
public static void main(String [] args)
{
helloMessage = "Hello, World!" ;
System.out.println(helloMeassge);
}
static String helloMessage;
}
Re: What is wrong with my code?
Quote:
Originally Posted by
dannyboi
...word for word from the book...
Either the book has a misprint, or you didn't quite get it:
Code java:
.
.
.
helloMessage = "Hello, World!" ; //<---Spelling #1
System.out.println(helloMeassge); //<---Spelling #2
.
.
.
static String helloMessage; //<---Spelling #1
Unlike the game of horseshoes (and unlike searches on Google), in Java, close doesn't count. Two out of three isn't good enough.
Cheers!
Z
Re: What is wrong with my code?
Quote:
Originally Posted by
Zaphod_b
Either the book has a misprint, or you didn't quite get it:
Code java:
.
.
.
helloMessage = "Hello, World!" ; //<---Spelling #1
System.out.println(helloMeassge); //<---Spelling #2
.
.
.
static String helloMessage; //<---Spelling #1
Unlike the game of horseshoes (and unlike searches on Google), in Java, close doesn't count. Two out of three isn't good enough.
Cheers!
Z
Ok then, I get the "message" part, but still I get:public class HelloApp
{
public static void main(String [] args)
{
helloMessage = "Hello, World!" ;
System.out.println(helloMessage);
}
static String helloMessage;
}
and this :
init:
deps-jar:
Compiling 1 source file to C:\Users\daniel\JavaApplication11\build\classes
compile-single:
run-single:
java.lang.NoClassDefFoundError: javaapplication11/HelloApp
Exception in thread "main"
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Re: What is wrong with my code?
Maybe because the object helloMessage isn't declaired a String until the program is done.
Re: What is wrong with my code?
Quote:
Originally Posted by
Firestar912
Maybe because the object helloMessage isn't declaired a String until the program is done.
Java actually doesn't care if you declare your variables below the code where they are used, so long as the scope is upward of their use.
For example:
Code JAVA:
public class Variable {
String myVariable = "Hello World!";
public someRandomBlockOfCode {
//....
}
public tryMe{
System.out.println(myVariable);
}
}
...is the same as:
Code JAVA:
public class Variable {
public someRandomBlockOfCode {
//....
}
String myVariable = "Hello World!";
public tryMe{
System.out.println(myVariable);
}
}
...is the same as:
Code JAVA:
public class Variable {
public someRandomBlockOfCode {
//....
}
public tryMe{
System.out.println(myVariable);
}
String myVariable = "Hello World!";
}
However, the following code will not work, because the variable is not within scope, or a higher bracket than the variables use
Code JAVA:
public class Variable {
public someRandomBlockOfCode {
String myVariable = "Hello World!";//Variable declared here
//....
}
public tryMe{
System.out.println(myVariable);//is not visible here
}
}
Re: What is wrong with my code?
Quote:
Originally Posted by
Firestar912
Maybe ....
Why would you say that? Have you tested it?
Bottom line: The code works just fine after the spelling typo on line 6 was corrected. The program compiles and executes perfectly on my systems if it is stored in a file named HelloApp.java. (I use command line javac and java.)
I didn't respond further to the Original Poster because I don't know how he is compiling it (IDE or whatever) so I can't offer additional help, and don't see any value in my trying to guess...
Cheers!
Z