Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it wrong to have static and non-static methods in the same class?

Is it wrong to have static and non-static methods in the same class?

like image 861
Ankur Avatar asked May 16 '09 12:05

Ankur


People also ask

Can a class have both static and non static methods?

You can have static methods inside a static class or a non-static class. Also, you can have a static constructor in a static class or a non-static class.

Can we have 2 static methods in a class?

The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters.

Can we overload static and non static method together?

Yes, we can declare an overloaded method as static and another one as non-static.

Can we call non static method in same class?

A non-static method is called ON an instance of a class, whereas a static method belongs to the class. Hope this helps. But I am working in Android doesn't onCreate create an instance of the class ? onCreate(){} creates an instance of the Bundle, but not your class.


1 Answers

Not really in regular java programming.

But if you're working extensively with dependency injection you probably have few or no static methods at all. In such a context it's fairly common to have only a few utility classes with static methods, and no other static methods.

like image 183
krosenvold Avatar answered Oct 24 '22 20:10

krosenvold