Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: beginner needs help with simple problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: beginner needs help with simple problem

    it's because the
    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.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: beginner needs help with simple problem

    Cheers thats a mistake i wont be making again.

Similar Threads

  1. Beginner Problem
    By Melvrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 15th, 2011, 12:29 PM
  2. Beginner. Need Help finishing a simple Shoot em up game ASAP!!!!
    By cbock55 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 08:13 AM
  3. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM
  4. beginner and need simple help. please!
    By alal12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 19th, 2010, 05:38 PM
  5. Beginner needs help with simple java assignment.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 07:53 PM