java - How to declare a method that returns an instance of an interface -


so bit new java. got introduced interfaces , have create method returns instance of interface chassis

below code:

public interface chassis {     public string chassis = "chassis";      public static void main(string[] args) {         public string getchassistype() {             return chassis;         }        

the problem is, keep getting error abstract methods cannot have body (as indicated blockquote) yet had not declared method abstract.

what seems problem?

you have 2 problems, can't put method inside method, , can't define method in interface in java. in java 8 can this

public interface chassis {      string chassis = "chassis";      default string getchassistype(){         return chassis;     } }   

i wouldn't define public static void main inside interface. while allowed now, developers find confusing. see @jürgen's answer, experienced developers believe.

i create class like

public class main {     public static void main(string... args) {         // anonymous subclass have create/call.         system.out.println(new chassis(){}.getchassistype());     } } 

Comments