Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By nonybrighto

Thread: Non static variable error

  1. #1
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Non static variable error

    import java.util.Scanner;

    public class total
    {
    public class gradebok{

    public void ownername(String name)
    {

    System.out.printf("this is your grade book %s ",name);
    }
    }
    public static void main(String[] args)
    {


    Scanner in = new Scanner(System.in);

    System.out.print("please enter your name here:");
    String enname= in.nextLine();

    gradebok boy= new gradebok();

    boy.ownername(enname);
    }

    i want to use dis code to display the message in the method ownername in gradebok class in main method but i get an error in main method "gradebok boy = new gradebok();" saying "non-static variable, this cannot be referenced from a static context" .... Pls wat do i do....i wanna correct dis b4 moving 2 next lesson

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: Non static variable error

    Please use [code] tags [/code] when posting code.
    Most people won't bother with a question in which the code is unformatted.

    Your inner class needs to be static ('public static class gradebok').
    The exact reasons are probably beyond your current level.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Non static variable error

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: Non static variable error

    Kk....thnx Guys i'll try it out.....u guys re gud!!!...thnx

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: Non static variable error

    And please don't use text speak.
    It's not as if you're charged by the character.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: Non static variable error

    Thanks a lot....it worked.....i've learnt a lesson ...i'll write in full now...:-D ..
    DarrylBurke likes this.

Similar Threads

  1. non static variable from static context error
    By chopficaro in forum New To Java
    Replies: 2
    Last Post: 06-04-2012, 02:44 PM
  2. Replies: 5
    Last Post: 12-01-2010, 07:43 AM
  3. Replies: 5
    Last Post: 10-15-2010, 05:21 AM
  4. Replies: 3
    Last Post: 02-11-2010, 10:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •