---
title: "Class Concepts in Java: Declaring Methods"  
description: "Our Point class declared here contains only fields. Now, we will add a method to the Point class which will help us understand the method declaration"  
author: "Ailsa Singh"  
published: 2016-05-21  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11115/class-concepts-in-java-declaring-methods  
category: "java"  
tags: ["java", "class"]  
reading_time: 5 minutes  

---

# Class Concepts in Java: Declaring Methods

Our Point class declared here contains only fields. Now, we will add a method to the Point class which will help us understand the method declaration and calling syntax.

```
class Point {    int x;    int y;}
```

The aim of adding a method to the class [definition](https://yourviews.mindstick.com/view/70598/false-furore-over-the-leadership-definition-statement-of-army-chief-bipin-rawat) is to give some [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) to it. The **functionality** we are going to add to our Point class is to determine the [distance](https://www.mindstick.com/blog/12028/what-distance-learning-is-not) of the point from the origin. Thus, we will add a [method called](https://www.mindstick.com/interview/2613/when-does-onresume-method-called) getDistance() that will return the distance between the point and the origin.

##### Point class with method declaration:

```
import java.util.*;
class Point {
             int x;             int y;
             double getDistance() {
                   return (Math.sqrt(x * x + y * y));
          }}
```

##### Test Class:

```
class TestPoint {          public static void main(String[] args)             {                   System.out.println("Create a Point object ... ");                   Point p1 = new Point();                   System.out.println("Initializing an object ...");
                   p1.x = 6;                   p1.y = 8;                   double distance = p1.getDistance();                   StringBuilder sb = new StringBuilder();                   Formatter formatter = new Formatter(sb, Locale.US);                   formatter.format("Distance of Point p1(" + p1.x + "," + p1.y
                                                                            + ") from origin is %.02f", distance);
                  System.out.println(sb);                   System.out.println();                   sb.delete(0, sb.length());                   System.out.println("Create another Point object ... ");                   Point p2 = new Point();                   System.out.println("Initializing an object ...");                   p2.x = 3;                   p2.y = 4;                   distance = p2.getDistance();
                   formatter.format("Distance of Point p2(" + p2.x + "," + p2.y
                                      + ") from origin is %.02f", distance);
                   System.out.println(sb);          }}
```

Output:

Create a Point object ...

Initializing an object ...

Distance of Point p1(6,8) from origin is 10.00

Create another Point object ...

Initializing an object ...

Distance of Point p2(3, 4) from origin is 5.00\

The Point class now has a method added to its definition. The method **getDistance** computes the point’s distance from the origin and returns a double value to the caller:

```
double getDistance() {return (Math.sqrt(x * x + y * y));}
```

The distance is computed using the **sqrt method** of the built-in Math class. In the main method of the TestPoint class, first we create an [instance](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code), p1, of the Point class and after this we initialize its [data members](https://www.mindstick.com/interview/2390/can-you-achieve-runtime-polymorphism-by-data-members-in-java) using the syntax we used in our earlier example. Then, we call the getDistance method to calculate the point’s distance from the origin:

double distance = p1.getDistance();

To invoke or call a method, we use the syntax **objectReference.methodName**. In this case, objectReference is the [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) to the Point object (that is, p1) and methodName is getDistance. To dump the object’s contents along with the distance, we use the [StringBuilder](https://www.mindstick.com/blog/99/stringbuilder-class-in-c-sharp) class:

```
StringBuilder sb = new StringBuilder();

Formatter formatter = new Formatter(sb, Locale.US);

formatter.format("Distance of Point p(" + p1.x + ","

                                                                        + p1.y + ") from origin is %.02f", distance);

System.out.println (sb);
```

After [building](https://www.mindstick.com/blog/23088/which-tonewood-is-suitable-for-building-a-guitar) the string, we print it to the console. To illustrate that we can create more than one Point object in our [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) using the same class definition, we have created another instance of the Point class called **p2**. The program initializes its fields to some values, computes its distance from the origin by calling the getDistance() method, and dumps it along with the object’s contents to the console.

As we can see, the same functionality (getDistance) is exhibited by both objects of the Point class. Thus, the **methods allow us to define a common functionality for all objects belonging to the same class type.**

---

Original Source: https://www.mindstick.com/blog/11115/class-concepts-in-java-declaring-methods

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
