So in the last tutorial, we learnt about how to define your
classes and how to define the packages to which they belong. In this tutorial,
we take things a notch further by learning how to create objects of your
classes.
So, let’s define a class called Person. The person we create will have an age, a name and an ID. These
are called properties of the class. We will also define a simple function that calculates
how old the Person objects will be
in ten years from now.
public class Person {
int age;
String name;
int id;
int
ageInTenYears () {
return
age + 10;
}
public
static void main (String[] args) {
}
}
Okay. So we’ve defined the class.
In the real world, I’m a person called Brian. I am 25 years
old and my ID is 11111111. You might be called Bob, with an age of 30 and an ID
of 22222222.
We define the new objects within the main function i.e.
within public static void main(String[]
args).
public class Person {
int age;
String name;
int id;
int
ageInTenYears () {
return
age + 10;
}
public
static void main (String[] args) {
Person
brian = new Person();
Person
bob = new Person();
}
}
So as you can see above, you type the name of the class, give
it an identifier or name, and then equate it to new Person();
What new Person(); is
call a constructor that creates the new object when you run the program. We
will talk about constructors in a future tutorial.
So, in the above code, we are telling Java to create a new
object of the Person class with the
identifier or label “brian” and another object of the Person with the identifier or label “bob.” When we were creating
the Person class, we said that the Person
would have an ID, a name and an age. So after creating “brian” and “bob”,
we know that they have a name, ID number and age. It’s only that we have not
yet defined the age and the ID.
We said that we want to tell Java that “brian” is 25 years
old with an ID number 11111111 and that “bob” is 30 years old with an ID number
22222222. How do we do this?
In Java, whenever you want to define the property of an
object, you use a dot (.) after the identifier or label that you gave your new
object. So if I want to tell Java that the age of the Person object labeled “brian” is 25, and that the ID of the Person object labeled “brian” is
11111111, I do this:
public class Person {
int age;
String name;
int id;
int
ageInTenYears () {
return
age + 10;
}
public
static void main (String[] args) {
Person
brian = new Person();
Person
bob = new Person();
brian.age
= 25;
brian.id
= 11111111;
}
}
Then, to tell Java that the Person object labeled “bob” has the age of 30 and the ID number is
22222222, we do the same as we did for the Person
object labeled “brian.”
public class Person {
int age;
String name;
int id;
int
ageInTenYears () {
return
age + 10;
}
public
static void main (String[] args) {
Person
brian = new Person();
Person
bob = new Person();
brian.age
= 25;
brian.id
= 11111111;
bob.age
= 30;
bob.id
= 22222222;
}
}
To test whether the objects are defined as we intended, we
can output the values to the console using the System.out.println() function like this:
public class Person {
int age;
String name;
int id;
int
ageInTenYears () {
return
age + 10;
}
public static
void main (String[] args) {
Person
brian = new Person();
Person
bob = new Person();
brian.age
= 25;
brian.id
= 11111111;
bob.age
= 30;
bob.id
= 22222222;
System.out.println(“Brian’s
age is ” + brian.age + “ and his id is” +
brian.id);
System.out.println(“Bob’s
age is ” + bob.age + “ and his id is” +
bob.id);
//Age in Ten years:
System.out.println("Brian's age in ten years
will be " + brian.ageInTenYears());
System.out.println("Bob's
age in ten years will be " + bob.ageInTenYears());
}
}
Figure 1: The figure shows the above code for the Person
class. The results are on the right.
I know I promised you in the last tutorial that we would also
create a Cat class and give the cat
an age, a weight and a height, I would like you to try this on your own. You
might encounter some difficulties, but keep at it until the program works. We
learn by failing our way to understanding.
In the next tutorial, we are going to look at access
specifiers. These are the keywords private,
public and protected.
If you have any questions or comment, make sure you drop them
in the comment section below and I will address them as soon as possible.
No comments:
Post a Comment