beginner needs help with simple problem
public class WelcomeName{
String myName="david";
public static void main(String[] args){
System.out.println("Welcome");
System.out.println(myName);
System.out.print("to the Java World");
}
}
As u can see from my code i am trying to print out;
Welcome
david
to the Java World
When I try to compile the program i get an error saying "non-static variable cannot be referenced from a static context".
I have searched for ages for a solution to no avail. Any help would be much appreciated.
Thanks in advance.
Re: beginner needs help with simple problem
it's because the
Code java:
public class WelcomeName{
String myName="david"; // this is put in the wrong spot, it should be within the main method
public static void main(String[] args){
System.out.println("Welcome");
System.out.println(myName);
System.out.print("to the Java World");
}
Actually, you can leave it out there but I don't think that you are that far in Java yet. Basically, you just need to move the variable into the main method, otherwise it won't be able to reference it.
Re: beginner needs help with simple problem
Cheers thats a mistake i wont be making again.