Main method is not static in class error [duplicate]

拈花ヽ惹草 提交于 2019-12-02 11:58:20

The signature of main requires static just like the error is telling you

public static void main (String[] args) {

And you didn't post ProcessHire but I think you wanted a new and perhaps to save the reference

ProcessHire ph = new ProcessHire(t, hc);

Java by default looks for a method

public static void main (String[] args) { }

or say

public static void main (String ...args) {}

args could be any name like public static void main (String ...arguments) {}

If you already have a public static void main method then you can have another main method which would act as normal method.

Now when you make the method static you get the other error because in a static context, calling a non static method(local to class) without initializing it's object would give error as java don't allow non static calls from static context/methods.

 non-static method error

One example solution is make ProcessHire method static:-

class UseTent
{
    Scanner keyboard=new Scanner (System.in);

public void main (String[] args)
    {

    Tent t= new Tent();
    HireContract hc = new HireContract();
    ProcessHire(t, hc);

    }

public static void processProcessHire(Tent tent,HireContract hireContract){
//your method definition
}
}

or if you can't make the method static then use approach below:-

class UseTent
{
    Scanner keyboard=new Scanner (System.in);

public void main (String[] args)
    {

    Tent t= new Tent();
    HireContract hc = new HireContract();
   new UseTent().ProcessHire(t, hc);

    }

public void processProcessHire(Tent tent,HireContract hireContract){
//your method definition
}
}

The main class should be public static void main (String[] args)

If ProcessHire method is static and in ABC class Try this,

class useTent{

    Scanner keyboard=new Scanner (System.in);

    public static void main (String[] args){

        Tent t= new Tent();
        HireContract hc = new HireContract();
        ABC.ProcessHire(t, hc);
   }
}

also follow Java Naming conventions.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!