Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Complex Classes Usage | Classes Advanced
Java Extended
course content

Conteúdo do Curso

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookComplex Classes Usage

Using class objects in another class

Now we will discuss the more complex usage of classes, specifically using class objects within another class.

Team and players

Let's imagine a situation where we have a Team class. Every team should have players. We could fill the players field with simple String values representing their names, but that wouldn't be the best practice. It would be much better to create a Player class with its own fields and methods and then create an array of Player objects within the Team class. Let's consider an example:

java

Team

copy
1234567891011121314151617181920212223242526
class Team { String title; String country; Player[] players; void play() { System.out.println("Team " + title + "is playing!"); } } class Player { String name; int age; int yearsOfExperience; public Player(String name, int age, int yearsOfExperience) { this.name = name; this.age = age; this.yearsOfExperience = yearsOfExperience; } void introduce() { System.out.println("Hi, my name is " + name + ", I am " + age + " years old and have " + yearsOfExperience + " years of experience"); } }

As you can see, we are using an array of Player objects in the players field of the Team class. From this, we can draw a few conclusions:

  1. We can create arrays of objects from our own created class;
  2. We can use objects of one class within another class to improve the overall logic.

But the question arises: How do we fill this array?

Answer: To do this, we need to create several Player objects and add them to the array of players. Let's create a Dream Team in the main method and see an example:

java

Main

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
package com.example; import java.util.Arrays; public class Main { public static void main(String[] args) { Player bob = new Player("Bob", 32, 5); Player alice = new Player("Alice", 28, 8); Player john = new Player("John", 41, 20); Player[] players = {bob, alice, john}; Team dreamTeam = new Team("Dream Team", "USA", players); System.out.println(dreamTeam); } } class Team { String title; String country; Player[] players; public Team(String title, String country, Player[] players) { this.title = title; this.country = country; this.players = players; } void play() { System.out.println("Team " + title + "is playing!"); } @Override public String toString() { return "Team{" + "title='" + title + '\'' + ", country='" + country + '\'' + ", players=" + Arrays.toString(players) + '}'; } } class Player { String name; int age; int yearsOfExperience; public Player(String name, int age, int yearsOfExperience) { this.name = name; this.age = age; this.yearsOfExperience = yearsOfExperience; } void introduce() { System.out.println("Hi, my name is " + name + ", I am " + age + " years old and have " + yearsOfExperience + " years of experience"); } @Override public String toString() { return "Player{" + "name='" + name + '\'' + ", age=" + age + ", yearsOfExperience=" + yearsOfExperience + '}'; } }

We created 3 objects of the Player class and initialized their fields through the constructor. Then we create an array with the Player type and add Bob, Alice, and John to it. Next, we create a Team object and initialize its fields through the constructor. We initialize the players[] field with the previously created array. We print the object to the console and see that the Team object has Player objects in the output.

Owner and pet

Let's look at another simpler example. Let's say we have an Owner and a Pet. We will create a separate class for each. The Pet class will have only one field - String name. The Owner class will have two fields - String name and Pet pet.

java

Pet

copy
123456789101112131415161718
class Pet { String name; public Pet(String name) { this.name = name; } } class Owner { String name; Pet pet; public Owner(String name, Pet pet) { this.name = name; this.pet = pet; } }

As you can see, these two classes are also connected, as the Owner class has a field of type Pet (which is the class we created ourselves!).

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 1
some-alt