Java Object Tutorial – Part 1

In this part we will learn about constructors, instances/objects and use get/set methods.


  • So to actually use our Person class in our application we need to create objects/instances of it
  • You maybe wondering, why didnt we just use the Person class instead we need to create Person objects/instances
  • Well class is only a template to create objects, when the application is running you have to use objects created according to that class. This also applies to other object oriented languages
  • Basically when we create a new class we define a template for our objects


  • Constructors are used to initiate the objects so that they can be used in a running application
  • In the previous code example even if we defined 2 objects they cannot be used, because they are just empty(null)
  • They need to be initialized with a constructor

Constructor with Parameters

  • In the above code ‘new Person()’ is called the default constructor
  • It does not assign any values to the object created, person1 still doesent have a name even if we did initialize it
  • We will define one new constructor for our Person class so that when we create a Person object, it will also have a name
  • It is ok if you use the default constructor, you can assign a name to it with setName method

Lets put it together

  1. Contructor with one parameter is used
  2. Default constructor is used, name assigned with setName
  3. Same as 2 but initialized later
  4. Do I need 3? Yes sometimes, you might know that you need a third person but dont know who it is yet


Q : Do I really have to work with objects?
A : Yes. Although there will be Classes that don’t need an object but they serve a different purpose, it’s not complex actually, it is just not usefull right now.

Recent Posts