There are times when programmers want to group a bunch of variables together. For example, if you wanted to talk about a doctor’s patient, you might want to store information about their first name, their last name, their address and their phone number. All of this information can’t be stored in one variable but it would be nice to have it grouped all together. This is what objects are for. This example declares an class to hold data about a person. Ideally, this class should be should be in another file in the same directory, as the program that is calling it.

public class persontype
{ //the instance variables of the class
//they are things that will be stored for each person
public String firstname;
public String lastname;
public int average;
//the constructor for a person class
//you can ignore this for now
public persontype ()
{ firstname = new String ();
lastname = new String ();
average = 0;
}
}
When a class is used in a program as a type for a variable, the variable is called an object. Here is the code to declare a persontype object and to put some data in it. Note that because it is an object, you must construct it.
persontype Dennis = new PersonType();
Dennis.firstname = "Dennis";
Dennis.lastname = "Ritchie";
Dennis.average = 72;
System.out.println(Dennis.firstname);