[JAVA] About class methods and static methods

We have summarized the class methods and static methods of Python, Ruby, and Java.

Python has class methods and static methods, Ruby has class methods but no static methods, and Java has no class methods but static methods.

The summary is as follows.

Python Ruby Java
Class method
Static method

Let's see how it works for each language.

Python

Python has class methods and static methods.

Neither class methods nor static methods can access instance variables.

python


class ClassSample:
  class_var = "hoge"

  @classmethod
  def class_method(cls):
    print "%s, class_var: %s" % (cls, cls.class_var)

  @staticmethod
  def static_method():
    print "%s, class_var: %s" % (ClassSample, ClassSample.class_var)

class SubclassSample(ClassSample):
  class_var = "foo"

ClassSample.class_method() # -> __main__.ClassSample, class_var: hoge
ClassSample.static_method() # -> __main__.ClassSample, class_var: hoge
SubclassSample.class_method() # -> __main__.SubclassSample, class_var: foo
SubclassSample.static_method() # -> __main__.ClassSample, class_var: hoge

In the case of an instance method, the instance is passed as the first argument, but in the case of a class method, the class is passed as the first argument. Therefore, the class method can access the class variables using the passed class.

Static methods are not passed a class, so you need to declare the class in order to access the class variables.

In the case of inheritance, the class variable is the value of the class variable of the child class because the child class is passed as the first argument in the case of the class method. On the other hand, in the case of a static method, the class variable is accessed by declaring the class, so the value changes depending on whether the declared class is a parent class or a child class.

The summary is as follows.

Access to instance variables Access to class variables Value of class variable at the time of inheritance
Class method Impossible Yes The value of a child class variable
Static method Impossible Yes if you declare your class Depends on the declared class

Ruby

Ruby has class methods.

Unlike Python, you can access instance variables. However, you can only access instance variables declared in class scope, not instance variables declared in non-class scope locations.

Also, because the scope of class variables is wide, if you rewrite a class variable in the inherited child class, it will be reflected in the parent class as well.

python


class ClassSample
  @instance_var = "hoge"
  @@class_var = "moge"

  def self.class_method
    puts "#{self}, instance_var: #{@instance_var}, class_var: #{@@class_var}"
  end
end

class SubclassSample < ClassSample
  @instance_var = "foo"
  @@class_var = "bar"
end

ClassSample.class_method # -> ClassSample, instance_var: hoge, class_var: bar
SubclassSample.class_method # -> SubclassSample, instance_var: foo, class_var: bar

The output of the class variable of ClassSample.class_method is bar instead of moge. This is because the class variable was rewritten at the inheritance destination.

The summary is as follows.

Access to instance variables Access to class variables Value of class variable at the time of inheritance
Class method Yes if declared in class scope Yes The value of a child class variable

Java

Java has static methods.

You cannot access instance variables, but you can access class variables. Java class variables are associated with classes as static variables, so they behave differently when inherited.

python


class ClassSample {
    static String classVar = "hoge";
    public static void staticMethod() {
        System.out.println("classVar: " + classVar);
    }
}

class SubclassSample extends ClassSample {
    static String classVar = "foo";
}

class Test {
    public static void main(String[] args) {
        ClassSample.staticMethod(); // -> classVar: hoge
        SubclassSample.staticMethod(); // -> classVar: hoge
    }
}

Even if you rewrite a class variable in the inherited child class, it will not be reflected in the parent class or child class.

The summary is as follows.

Access to instance variables Access to class variables Value of class variable at the time of inheritance
Static method Impossible Yes The value of the parent class variable

Recommended Posts

About class methods and static methods
About _ and __
Class method static method
Talking about Python class attributes and metaclasses
About cross-validation and F-number
About Python and regular expressions
About handling Django static files
Class inheritance and super functions
About Python and os operations
Python # About reference and copy
About Numpy array and asarray
About Python sort () and reverse ()
perl objects and python class part 1.
Python classes and instances, instance methods
About class methods and static methods
Class method static method
Class inheritance and super functions
perl objects and python class part 1.
class
Python classes and instances, instance methods
Talking about Python class attributes and metaclasses