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

class, instantiation, java, methods, oop

Solution

It's called static variables and static methods. Just try it and see that it compiles.

Problem

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 creation of an instance of the class. For example, I can do something like that: ``` NameOfClass.doMethod(x1,x2,...,xn) ``` In general I do not see why it should be impossible. I just call a function which does something (or return some values). If it is possible, what will happen if the method sets a value for a private variable of the class. How can I reach this value? In the same way? ``` NameOfClass.nameOfVariable ```

Original source

Related problems