Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 245
Roman Avatar asked Mar 06 '10 21:03

Roman


1 Answers

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

like image 61
Yoni Avatar answered Sep 29 '22 01:09

Yoni