Results 1 to 4 of 4
- 02-10-2010, 11:28 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
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 ?
- 02-10-2010, 05:11 PM #2
Senior Member
- Join Date
- Mar 2009
- Location
- USA
- Posts
- 127
- Rep Power
- 0
your main method is static but enum is non-static. make it static and try.
-
The methods should not be in the enum declarations but in the enum itself:
Whatever you do, don't declare anything static here. Aseem meant well, but it's wrong advice.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()); } }Last edited by Fubarable; 02-10-2010 at 06:08 PM.
- 02-11-2010, 09:59 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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
-
non-static method cannot be referenced from a static context.
By blackstormattack in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:05 AM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
non-static member can not be referenced from a static context
By christina in forum New To JavaReplies: 3Last Post: 03-20-2009, 12:35 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks