How to use nonstatic method inside the class without an object
He has used isEmpty() method which is non static without creating an object in if(!isEmpty) how this is possible
He calls the
isEmpty method within
dispense which is itself a non-static method. That means that there is no way to run that code without first creating an instance of the class.
When Java encounters a call to a method that is found within the object it is treated as a call to that object's method. So
isEmpty() is equivalent to
Referencing a non-static method or field variable within a class is only an issue if the code is written inside a static method, within non-static methods it poses no problem since an object is guaranteed to exist when the code is ran.