Java - Class - Methods

Java

In this tutorial we will learn about methods of a class in Java programming language.

In the previous tutorial Class - Member Variables we learned how to create member variables inside a class and also covered about the access modifier that controls the accessibility of a class variable.

Now, lets talk about methods that operates on the member variables of a class.

Method syntax

Following is the syntax of method declaration in Java.

access_modifier return_type method_name(list_of_parameters) {

  // body of the method

}

Where, access_modifier is some access modifier that defines the accessibility of the method.

return_type defines the type of data returned by the method.

method_name is a valid name given to the method.

list_of_parameters is a list of parameters that tells us about the arguments that the method can accept.

The body of the method starts from { opening curly brackets and ends with } closing curly brackets.

Naming methods

Following are the points that we need to keep in mind when naming methods.

  • Use a-z, A-Z, 0-9 and underscore _ characters when naming methods.
  • Use simple and descriptive names.
  • Can't start a method name with digits.
  • Avoid acronyms unless it is necessary and reasonable.
  • Avoid keywords like for, if etc.

Following are some valid method names.

findAverage

computeVolume

printResult

add_user_to_team

Following are invalid method names.

// can't start with digit
1group

// character - is not allowed in method name
super-user

// can't use space
Sample UserDetail

Return type of methods

Return type of a method tells us about the type of data returned by the method.

Return type can be any of the fundamental data types of Java like char, int, double etc.

If the method is not going to return any value then we set the return type to void.

Access modifier of a method

access modifier defines the accessibility of a method and following are the access modifier that we can add to a method.

public

If we set the access modifier of a method to public then it is accessible from both inside and outside of the class.

private

When we set the access modifier of a method to private then it can only be accessed from within the class.

protected

The protected access modifier makes the method accessible both inside the class and in the classes that inherits it.

More on inheritance in the inheritance tutorial.

The this keyword

We use the this keyword inside the class to access the members of the class.

So, for example if we want to access a member variable say length from inside a method say getLength() then we can write the following this.length and it will give us the length of the PackagingBox class.

Adding methods to PackagingBox class

In the Introduction to Class tutorial we have created the PackagingBox class and added some member variables in the previous tutorial.

So, our class code looks like the following.

class PackagingClass {

  // member variables
  private double length;
  private double breadth;
  private double height;
  public double volume;
  double weight;
  double price;
}

Now, lets add some methods to the class.

Example: Creating getter and setter methods

To get and set the value of the length, breadth and height variable we can write the following methods.

public void setLength(double length) {
  this.length = length;
}

public double getLength() {
  return this.length;
}

public void setBreadth(double breadth) {
  this.breadth = breadth;
}

public double getBreadth() {
  return this.breadth;
}

public void setHeight(double height) {
  this.height = height;
}

public double getHeight() {
  return this.height;
}

Note! In the set methods we are passing an argument which we are then assigning to the member variables.

Similarly, we can create get and set methods for the weight and price variables.

public void setWeight(double weight) {
  this.weight = weight;
}

public double getWeight() {
  return this.weight;
}

public void setPrice(double price) {
  this.price = price;
}

public double getPrice() {
  return this.price;
}

For the volume we can create a method that will compute the volume of the box and another method that will return the volume.

public void computeVolume() {
  this.volume = this.length * this.breadth * this.height;
}

public double getVolume() {
  return this.volume;
}

Our complete code for the class will look like the following.

class PackagingClass {

  // member variables
  private double length;
  private double breadth;
  private double height;
  public double volume;
  double weight;
  double price;

  // methods

  //---- get and set length

  public void setLength(double length) {
    this.length = length;
  }
  
  public double getLength() {
    return this.length;
  }

  //---- get and set breadth
  
  public void setBreadth(double breadth) {
    this.breadth = breadth;
  }
  
  public double getBreadth() {
    return this.breadth;
  }

  //---- get and set height
  
  public void setHeight(double height) {
    this.height = height;
  }
  
  public double getHeight() {
    return this.height;
  }

  //---- get and set weight
  
  public void setWeight(double weight) {
    this.weight = weight;
  }
  
  public double getWeight() {
    return this.weight;
  }

  //---- get and set price
  
  public void setPrice(double price) {
    this.price = price;
  }
  
  public double getPrice() {
    return this.price;
  }

  //---- compute and get volume

  public void computeVolume() {
    this.volume = this.length * this.breadth * this.height;
  }

  public double getVolume() {
    return this.volume;
  }
}