1

I am learning java and was trying out interface and its implementation and I have a few errors which I dont understand.

Mainly I dont understand the non static method cannot be called in static context. Can someone help me and explain the problem?

CODE:

 import java.io.*;
import java.util.Scanner;
 interface test
 {
String inmarks(int x);
default  void remark(String input)
    {
        switch(input)
        {
        case "A" :
            System.out.println("very good");
        case "B":
            System.out.println(" good");
        case "C":
            System.out.println("avg");
        case "D":
            System.out.println("below average");
        case "F":
            System.out.println("poor");
        }
    }
static void karuthu()
{
    System.out.println("marks doesnt matter");
}
}

 public class interfacemain implements test
{
public String inmarks(int x)
{
    if (x<50) return "F";
    else if (50<=x && x<=70) return "D";
    else if (70<=x && x<=80) return "C";
    else if (80<=x && x<=90) return "B";
    else if (90<=x && x<=100) return "A";
}
public static void main(String args[])
{

int mark;
String grade;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your marks da ");
mark =sc.nextInt();
grade=inmarks(mark);


 
System.out.prinln("Your marks is`"+ mark+"/n Your grade is "+ grade+"/n Your remark 
 is"+ remark(grade));
 test.karuthu();
}
 }

ERROR:

interfacemain.java:46: error: non-static method inmarks(int) cannot be referenced from a static context
grade=inmarks(mark);
      ^
interfacemain.java:50: error: non-static method remark(String) cannot be referenced from a static context
System.out.prinln("Your marks is`"+ mark+"/n Your grade is "+ grade+"/n Your remark is"+ remark(grade));
                                                                                         ^
interfacemain.java:50: error: 'void' type not allowed here
System.out.prinln("Your marks is`"+ mark+"/n Your grade is "+ grade+"/n Your remark is"+ remark(grade));
                                                                                               ^

3 errors

James Westgate
  • 11,306
  • 8
  • 61
  • 68
Mukunth
  • 11
  • 2
  • Does this answer your question? [Android: Non-static method cannot be referenced from static context. Confused?](https://stackoverflow.com/questions/28044873/android-non-static-method-cannot-be-referenced-from-static-context-confused) – Stultuske Feb 11 '22 at 07:26
  • so should i use a object of the class and give input as object.variable? sorry im new to java – Mukunth Feb 11 '22 at 07:40
  • Yes. You should call these methods via an instance. As the methods are non-static, calling them from the main method that is static is not possible. This is because non-static methods or instance methods are always tied to an instance that calls them. So, you should, typically, define a class that implements the interface and then call these methods via an instance of that class. – vivek Feb 11 '22 at 08:15
  • ohh got it thanks a lot @vivek – Mukunth Feb 11 '22 at 08:53

0 Answers0