Showing posts with label constructor. Show all posts
Showing posts with label constructor. Show all posts

Saturday, 7 February 2015

Java "this" Prefix

If you have a class and a constructor method with parameter(s), you might want to use the same names for the variables in the class as you use for the parameters passed to the method. To distinguish between the two, you can prefix the variables belonging to the class with the word this. In the example below, I create a class called Square. It has a variable called width. The constructor method accepts a parameter with the same name. It then displays the value of the parameter and the initial value of the class variable. After that, it assigns the parameter value to the class variable and redisplays the class variable to show that the assignment has worked: 

Java > cat Square.java
class Square
  {
  double width;
  Square (double width)
    {
    System.out.println("width = " + width);
    System.out.println("this.width = " + this.width);
    System.out.println("Assigning parameter value");
    this.width = width;
    System.out.println("this.width = " + this.width);
    }
  }
Java > javac Square.java
Java > cat SquareExample.java
public class SquareExample
  {
  public static void main(String args[])
    {
    Square s1 = new Square(5);
    }
  }
Java > javac SquareExample.java
Java > java SquareExample
width = 5.0
this.width = 0.0
Assigning parameter value
this.width = 5.0
Java >

Wednesday, 4 February 2015

Overloaded Java Constructors

The example below shows a class with an overloaded constructor:

Java > cat Rectangle.java
class Rectangle
  {
  double width;
  double height;
  Rectangle()
    {
    width = 2;
    height = 3;
    }
  Rectangle(double w, double h)
    {
    width = w;
    height = h;
    }
  }
Java > javac Rectangle.java
Java >

If you have a class like this, when you create members, you have a choice. You can accept the default settings or you can override them by supplying parameters. In the example below, r1 takes the default settings whereas r2 uses parameters to change the width to 4 and the height to 5:

Java > cat RectangleExample.java
public class RectangleExample
  {
  public static void main(String args[])
    {
    Rectangle r1 = new Rectangle();
    System.out.println("r1 width = " + r1.width);
    System.out.println("r1 height = " + r1.height);
    Rectangle r2 = new Rectangle(4,5);
    System.out.println("r2 width = " + r2.width);
    System.out.println("r2 height = " + r2.height);
    }
  }
Java > javac RectangleExample.java
Java > java RectangleExample
r1 width = 2.0
r1 height = 3.0
r2 width = 4.0
r2 height = 5.0
Java >

Tuesday, 3 February 2015

Java Constructors

If you have a class called, for example, Rectangle, you can add a method to it with the same name as the class. This method must have no return type, not even void. It is called a constructor. Here is an example:

Java > cat Rectangle.java
class Rectangle
  {
  double width;
  double height;
  Rectangle()
    {
    width = 2;
    height = 3;
    }
  }
Java > javac Rectangle.java
Java >

The purpose of a constructor is to give default values to a class member when it is created. You can see what I mean below, where an instance of Rectangle is created. It automatically has a width of 2 and a height of 3:

Java > cat RectangleExample.java
public class RectangleExample
  {
  public static void main(String args[])
    {
    Rectangle r1 = new Rectangle();
    System.out.println("width = " + r1.width);
    System.out.println("height = " + r1.height);
    }
  }
Java > javac RectangleExample.java
Java > java RectangleExample
width = 2.0
height = 3.0
Java >