1

I've started learning java. I've understood so far that method calls are:

object.methodname(arguments)

although I think more broadly I've discovered that it's actually:

receiver.methodname(arguments)

and that a receiver might not necessarily be an object. or perhaps my understanding of object is wrong. I get that a class defines a type, and an object is an instance of the class. And that calling a method is sending a message to an object, and the message has to be something the object understands as defined by the methods in its class.

But... what about:

import java.lang.Math
double x = 16;
double y = Math.sqrt(x);
double z = Math.pow(x,y);

is Math an object? I don't believe so. My understanding so far is that Math is a "package class" in the java.lang package. I don't yet really understand what a "package class" is other than that it's some kind of pre-written library of functions I can use...

But if it's a class, then that means sometimes a receiver can be a class and doesn't necessarily have to be an object...?

...or is Math an object/instance of some other class?

I've googled every combination of java/receiver/object/class/etc that i can think of and can't find anywhere that really clears this up for me. I'd appreciate any insights anyone can offer me to clear it up.

Thanks!

DavidT
  • 655
  • 7
  • 19
  • `Math.sqrt` is a `static` method. See [*Understanding Class Members*](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) in *The Java Tutorials*. – 5gon12eder Feb 01 '15 at 04:11
  • 1
    A receiver the thing that receives the call for one of its methods. It can be a package or an object. – MarsAtomic Feb 01 '15 at 04:22

1 Answers1

3

Math is a class! But those are static methods :) which means Math does not need to be instantiated as an object to be used. You just use it directly.

Packages are just ways to organize/categorize classes.

"receiver" is not common jargon in Java and O.O.

Luís Soares
  • 5,726
  • 4
  • 39
  • 66
  • Ok, this concept of using methods in a class really threw me. Sounds like you're saying it's not necessarily because Math is some special kind of class, but any class can have "static" methods that can be called on via the class without having to instantiate it to an object to use. Although, a bit more googling seems to suggest that Math is in fact a special kind of class - a static class (although it seems the word is "final" rather than "static"?) - that contains only static members. Instantiating such a class wouldn't really make sense, and this accommodates that. Have I got it? Thanks! – DavidT Feb 01 '15 at 04:22
  • You're on the right track. every Java keyword makes something special.. "final" just means Math cannot be inherited. Be aware that "public final class Math" is the definition of that class: only its methods are static. Also take into account that static classes are just utilities/helpers, They are not objects.. don't give them so much importance in a O.O. world. – Luís Soares Feb 02 '15 at 00:51
  • Luís, I came up with what I said before about static classes from the green ticked answer here: [link](http://stackoverflow.com/questions/7486012/static-classes-in-java) but realize now I misread it. He was describing how to **simulate** what other languages call a static class and the "Declare ... `final"` bit (to stop it from being inherited from) is just one of the steps. Between his explanation and yours it makes perfect sense now, so much thanks to you. I'd up-vote you if I could but I'm new here and apparently I don't have enough "reputation" yet. ;) – DavidT Feb 02 '15 at 07:26
  • 1
    Javadoc for Class method isAssignableFrom calls the "object" a receiver. "Returns: true the argument can be assigned into the receiver false the argument cannot be assigned into the receiver" – Eric Nov 06 '15 at 16:31