Creating constructors

In this section, you're going to learn about a very special member we can assign to our custom classes, that is, the constructor. To start off, let's take a look at the following code:

package gettingobjectoriented; 
 
import java.util.*; 
 
public class GettingObjectOriented { 
    public static void main(String[] args) { 
        Scanner reader = new Scanner(System.in); 
        Person john = new Person(); 
        john.firstName = "John"; 
        john.lastName = "Doe"; 
        john.birthday = new GregorianCalendar(1988,1,5); 
        System.out.println( 
            "Hello my name is " +            
            john.fullName() + 
            ". I am " + 
            john.age(new GregorianCalendar()) + 
            " years old."); 
    } 
} 

This program creates an instance of our custom class Person and immediately assigns some values to member variables of ...

Get Java Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.