RSS Feed

Constructor

Constructor Constructor

A constructor is a special method for initializing a new instance of a class.

The constructor method for a class will have the same name as the class.

Constructor Example

This example shows a class and a constructor named Circle:

public class Circle {
 public int x = 0;
 public int y = 0;
 public int radius = 0;
 public Circle(int x, int y, int radius) {
 this.x = x;
 this.y = y;
 this.radius = radius;
 }
}

A class may have multiple constructors. In this case, each constructor will have the same name, but will have different arguments.

A no-argument constructor is a constructor which does not take any arguments.

Related Articles
  1. Java Method
  2. Java Class
  3. Object
Leave a Reply

Post your comments and questions below, but please follow our commenting guidelines.


Path: Home > Programming > Java > Constructor