Menu Close


Java: Objects

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:

Ussing a class:

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


  • 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:

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