Results 1 to 4 of 4
  1. #1
    pictianpravin is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Unhappy non-static variable grade cannot be referenced from a static context

    public enum Scale2
    {
    GOOD('C') { public char getGrade() { return grade; } },
    BETTER('B') { public char getGrade() { return grade; } },
    BEST('A') { public char getGrade() { return grade; } };

    private char grade;

    Scale2(char grade)
    {
    this.grade=grade;
    }

    // Insert your code here

    public static void main(String[] args)
    {
    System.out.println(GOOD.getGrade());
    }
    }

    /* which following code when inseted at line shown above will make prog print C choose two correct answers*/

    a ) public char getGrade() { return grade;}
    b) public int getGrade() { return grade;}
    c) abstract public int getGrade();
    d) abstract public char getGrade();

    /* when i compile and run above program it gives error as below for all option as answer */
    C:\Scale2.java:3: non-static variable grade cannot be referenced from a static context
    GOOD('C') { public char getGrade() { return grade; } },
    ^
    C:\Scale2.java:4: non-static variable grade cannot be referenced from a static context
    BETTER('B') { public char getGrade() { return grade; } },
    ^
    C:\Scale2.java:5: non-static variable grade cannot be referenced from a static context
    BEST('A') { public char getGrade() { return grade; } };
    ^
    3 errors

    Process completed.

    how do i make this program work ?

  2. #2
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    your main method is static but enum is non-static. make it static and try.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The methods should not be in the enum declarations but in the enum itself:
    Java Code:
    public enum Scale2 {
      GOOD('C'),  BETTER('B'),
      BEST('A');
    
      private char grade;
    
      Scale2(char grade) {
        this.grade = grade;
      }
      
      public char getGrade() { return grade;}
    
    
      public static void main(String[] args) {
        System.out.println(GOOD.getGrade());
      }
    }
    Whatever you do, don't declare anything static here. Aseem meant well, but it's wrong advice.
    Last edited by Fubarable; 02-10-2010 at 06:08 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Um...it's a test.
    The code as supplied isn't intended to compile.
    You're supposed to figure out which 2 of a,b,c or d will get it to work.

    And I'm not going to tell you which two it is...:)

    ETA: Bugger...I sit corrected!

    They've made an error in the code...that "grade" should be protected...
    Last edited by Tolls; 02-11-2010 at 10:03 AM.

Similar Threads

  1. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  2. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  3. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09: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
  •