Search code examples
javamethodssubroutine

When is a Java subroutine not a method?


I came across this during learning Java in the beginning, but I am learning top down, so I would like some direction:

This was on a Java tutorial relatively early on:

As one final general note, you should be aware that subroutines in Java are often referred to as methods. Generally, the term "method" means a subroutine that is contained in a class or in an object. Since this is true of every subroutine in Java, every subroutine in Java is a method (with one very technical exception). The same is not true for other programming languages. Nevertheless, the term "method" is mostly used in the context of object-oriented programming, and until we start doing real object-oriented programming in Chapter 5, I will prefer to use the more general term, "subroutine." However, I should note that some people prefer to use the term "method" from the beginning.

My question is what is that "one very technical exception." Since I am learning multiple stack technologies, I would like to know this specific exception he is referring to. It comes from this tutorial : http://math.hws.edu/javanotes/c2/


Solution

  • Quoting from chapter 5 of the tutorial you linked to:

    Constructors are subroutines, but they are subroutines of a special type. They are certainly not instance methods, since they don't belong to objects. Since they are responsible for creating objects, they exist before any objects have been created. They are more like static member subroutines, but they are not and cannot be declared to be static. In fact, according to the Java language specification, they are technically not members of the class at all! In particular, constructors are not referred to as "methods."