# Class (or Static) Methods

• Introduction

• In the previous webpage, we learned about the Mathematical Functions (such as sin(x), cos(x), log(x)...) that are defined in the Java class called Math

• When we wanted to compute the square root of a number x,   we write:

 ``` Math.sqrt(x) ```

• Question

Is this the way we that we have learned to invoke a method ?

NO !!

We have learned how to invoke methods on objects in this webpage: click here )

The above method call DOES NOT FIT the syntax that we learned in that webpage....

In order to "fit" the above method call in the syntax of a methods on objects, we will have to make the following associations:

 ``` Math.sqrt(x) ^ ^ ^ | | | | | +--- Paramter | +------- Method name +------------ Object ```

But Math is a class and NOT an object

• Here is an example that highlight the difference between the call Math.sqrt(x) and the usual way that we use method calls.

Example Program:

• You can review the difference between a class and an object in these webpage:

• Class Methods

• The sqrt() in the class class is an example of a class method

A class method has a special form: it does not operate on an object

• Example: compute the square root of a variable x

What we would write IF we follow the syntax of an ordinary method call: The correct way to express the operation to take the square root of the number x
```
double x = 4;

double root = x.sqrt(); // Wrong
```
```
double x = 4;

double root = Math.sqrt(x); // Good
```

• The square root function works on numbers

However, a number is NOT an object.

Therefore, the square root function does not work on objects.

• Now is the time to tell you that there are 2 categories of methods:

• Instance methods that operate on objects (we have seen these before)

I will repeat the syntax used to call an instance method for the sake of contrast:

 ``` objectName.methodName( parameters ) Example: Rectangle box = new Rectangle(5, 15, 20, 30); box.translate(2, 3); ```

• Class methods that DO NOT use objects

Syntax that you need to use to call a class method is:

 ``` ClassName.methodName( parameters ) Example: Math.sqrt(4); ```

• NOTE:

The notation:

 ``` Math.sqrt(4); ```

makes it appears as if the sqrt() method is applied to an "object" called Math - because Math preceeds sqrt() just like as box preceeds translate() in the method call box.translate()

The difference between box.translate(..) and Math.sqrt(..) is:

• box is an object - it is made with the new operation

• Math is an class - we did NOT use any new operation

• Static method

• A class method is also known as a static method

• This weird terminology is a remnant of the C++ root of Java (Java was based on C++)...

The guy who designed the Java programming language was heavily influenced by C++.

• But you can forget about the C++ history, just remember that:

 The term static has nothing to do with the ordinary English meaning (stationary) Static methods do NOT operate on objects In other words: static methods do NOT have a (hidden) implicit parameter (To refresh your memory on the (hidden) implicit parameter this: click here) In other words: static methods use ONLY have explicit parameter So a static method is "self-contained" - it does NOT use an object to operate.

• Instance method

• Methods that DO operate on objects (i.e., NON-static) are called instance method

 The object is passed to an instant method by means of the implicit parameter !!!

• We have defined and used instance methods before:

• the translate() instance method in class Rectangle is an instance method (no "static" keyword)

• Other instance method are those defined in the BankAccount class:

• deposit()
• withdraw()
• getBalance()

• How can I tell if a method is a class or an instance method ?

• Class method is always defined using the keyword static

• Instance method is always defined WITHOUT using the keyword static

• When you read Java's API documentation, the keyword static before the method name tells you that the method is a Class method

Method names without the keyword static before its name tells you that the method is an instance method

• Check out the API documentation on: