Class methods in Ruby vs Static methods in Java

Aayush Sharda
Jul 25, 2017 · 3 min read

For a fresh grad just out of college, it is common to have a language of preference to code in. It is difficult to hear someone disparaging the language of your choice, but getting attached to any particular language serves you poorly in long run. Recently, at my bootcamp at Go-jek I was made to see the shortcomings of Java which would have been difficult for me to spot had I been rigid over my preference.

Let’s first get on to understand what class methods in Ruby are. Ruby considers almost every entity as an object, say for instance an object of a class Ball is an object of type Ball and the class Ball is itself an object of type Class. I know this might not be easy to take in for the Java lovers, it blew my mind at first!

Now, the class methods in Ruby are the methods called by the object “Ball”, unlike instance methods which are called by the objects of the object “Ball”. Let me explain this to you via example:-

class Ball
#class method
def self.find()
print "Finding ball"
end
#instance method
def brand(brand_name)
print "Brand of the ball: #{brand_name}"
end
end

The class method can only be called by the object Ball(class) whereas the instance method can only be called by the objects of Ball.

~Ball.find()
=> Finding ball
~ ball = Ball.new
~ ball.brand(Wilson)
=> Brand of the ball: Wilson
~ ball.find()
=> NoMethodError: undefined method 'find

I hope it is now clear to you what class methods in ruby are. Are you able to spot the similarity between the class methods in Ruby and static methods in Java? For those who are new to Java as well, static methods in Java can be called directly from the class, like the class methods in Ruby. But the catch here is that the static methods can also be called by the objects of the class in Java(and mind it the class in Java is not an object).

To elaborate over the inconsistency let’s discuss about the static methods first. Static method/functions are stateless. That is, they act only on information

  1. provided by arguments passed to the method/function
  2. in static variables
  3. that is hard-coded into the method/function (e.g. you create a static function to return “hello” — then “hello” is hard-coded into the function).

For such a method, it is impossible to change the state of the object calling it. So, technically the changes made by any object to the state of the class do not depend on the object calling the method and any object calling the method will result into the same state for the class. So, why did Java allow the objects to call the static methods? This was just to cover up on the shortcoming of Java which says that every function should be called by an object. So, in order to incorporate this exception where ideally the function should only be called by the class, introducing static function calling by objects superficially hides this drawback.

Care to guess how Ruby handles this? As mentioned above, for Ruby a class is an object, so it is suffice to say that the class method can only be called by a class(object) and not by the instances of that class.

Aayush Sharda

Written by

Product Engineer @ Go-Jek

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade