Java is an object-oriented language and not a procedural or functional language. It is also an imperative language. Therefore we have to understand how objects are created and what object-oriented means.

 

This topic is pretty much the base of all Java programming. Which is why this post will have to be longer than the other posts to fully explain this concept.

Example of a class:

//This is a class method, notice that it has no main method
//We use this class in the tester one bellow

public class MyPoint
{
 private int x;//instance variables
 private int y;

 public MyPoint()//no-arg cst 
 {
 this.x = 0;
 this.y = 0;
 }
 
 public MyPoint(int x, int y)
 {
 this.x = x;
 this.y = y;
 }

 //getter method, with int return type
 public int getX()
 {
 return this.x;
 }
 
 public int getY()
 {
 return this.y;
 }
 //sets the instance variables
 public void setXY(int x, int y)
 {
 this.x = x;
 this.y = y;
 }
 
 public String toString()
 {
 return String.format("(" + this.x + "," + this.y + ")");
 }
 
 //This operation finds the distance between the specified point
 //and the objects current point
 public double distance(int x, int y)
 {
 return Math.sqrt(Math.pow(this.x - x, 2) + (Math.pow(this.y - y, 2)));
 
 }
 
 //this actually passes in an object as a parameter
 //Therefore we must use the getters to retrieve X and Y
 public double distance(MyPoint pt)
 {
 return Math.sqrt(Math.pow(pt.getX() - this.x, 2) + (Math.pow(pt.getY() - this.y, 2)));
 }
 
}

Ussing a class:

public class MyPointTest
{
 public static void main(String[] args)
 {
 //this invokes our first contructor
 MyPoint p1 = new MyPoint();
 //this invokes our second constructor
 MyPoint p2 = new MyPoint(4, 8);
 
 double dis = p1.distance(3, 4);
 System.out.print("The distance from " + p1.toString());
 System.out.println(" to (3, 4) is: " + dis);
 
 p1.setXY(10, 10);
 System.out.println("x is: " + p1.getX());
 System.out.println("y is: " + p1.getY());
 
 MyPoint[] points = { p1, p2, new MyPoint(7, 3) };
 for (int i = 0; i < points.length - 1; i++)
 {
 System.out.print("The distance from " + points[i].toString());
 System.out.print(" to " + points[i + 1].toString() + " is: ");
 dis = points[i].distance(points[i + 1]);
 System.out.println(dis);
 }
 
 }
}

The way Objects/Classes work

  • A Class is like the blueprint to creating something, for example a movie
    • The blueprint has criteria for what the movie contains
    • For Example some properties a movie might have are:
      • Title
      • Rating
      • Run-time
      • Director
  • Once we have a blueprint for making a movie, we can create a movie
    • A movie is an Object that is in the Class “movie”
    • I can make two movies, and they both have different properties, but they are both movies
    • An object is also known as an Instance

Instance Variables

  • The properties I listed are defined by the class
  • However those properties belong to each individual movie
    • So the Title: “Phantom of the Opera” belongs only to that movie object

Constructor

  • Simply put these guys make your object and put it in memory
  • You get to define your constructor
    • This can be used to set the properties of your object
  • You can have multiple types of constructors

Accessor modifiers/Encapsulation

  • You will see that at the top of our program the x, and y instance variables are have “private” before their data type, Why is that?
    • This is called Encapsulation, the brief of what it does is that it stops someone from changing/accessing a variable unless they go through some extra steps
    • This is done to add extra security to your code
  • Getters/Setters
    • This is the extra step I was talking about, if you want to get or change the value of an instance variable, its good practice to make these methods
    • A getter
      • Has a return type
      • Takes no parameters
      • Returns the value of the instance variable
    • A setter
      • Has no return (void)
      • Has at least one parameter
      • sets the value of an instance variable

This keyword

this is a keyword that represents the object, which indicates the compiler to use the method on the objects values.

If a movie with the tittle “The Matrix” uses the method getTitle(), this method has the following code:

public String getTitle()
{
 return this.title;
}

this.title will refer to the title of the movie that is calling the method.

  • For this movie, it returns the value “The Matrix”
  • If the movie has the title “Nacho Libre” it will return that title.

This explains for short how to declare a class and use the class to create an object.

See also: Oracle documentation