Can I use methods of a class without instantiating this class?

前端 未结 11 2083
执念已碎
执念已碎 2020-12-15 04:10

I have a class with several methods and there is no constructor among these methods.

So, I am wondering if it is possible to call a method of a class without a creat

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 04:45

    I have a class with several methods and there is no constructor among these methods.

    If you don't explicitly define a constructor then you get a default constructor provided by the compiler. So if those methods aren't static, try this:

    NameOfClass x = new NameOfClass();
    x.doMethod(x1,x2,...,xn);
    

提交回复
热议问题