Last Updated: April 09, 2021
· JustSpecialist

Functions in your own class

The best example of usage of your own class is to work with a database. Moreover, using functions in class is more correct than simple functions anyway.
Let's work on the example of databases.

What is a database?

Imagine some tour-company website, where you see many tour names, dates, prices and so on. So a simple mass is not good enough to keep all this information.

Step 1.

Decide which values you have.
Name of tour - string
Date - can be string if you don't need to make changes, but better to use calendar/date.
Price - double

Step 2.

Make a class with these values.

As we have a lot of string values, we need a mass of string, hopefully, everyone understands it.
class Tour_Data_Base () {

String[] name = new String[any length];

String[] date = new String[any length];

double[] price = new double[any length];

Step 3.

Start making functions below the initialization.

First function must be reading to complete our database.

Functions for reading you can find in previous articles, but here there is a little difference.

It is incorrect to write just the name of variable, cause it is in your class, so you must write the "direction"
Instead of name=...; write;

Step 4.

Call functions written in your class correctly.

Initialize the object of your class.

Tour_Data_Base data = new Tour_Data_Base();

If your function is with empty brackets, then when you call it, your brackets are also empty.

void Read_File () { ... }

In Main:


If the brackets are not empty, then in the Main function, when you call a class function, they are also not empty.

void Read_From_Console (String name, String date, double price) { ... }

A small tip: name that you send to a class function is not the same as the name initialized in your class function.
So here you have to initialize these variables (String name, String date, double price) in Main to send to the class function.

Scanner in = new Scanner(;

String name = in.nextLine();

String date = in.nextLine();

double price = in.nextDouble();


I hope it was useful for those who want to take their first steps. If you have any questions, write to me anytime.

Good luck in your job!

Written by AndersenLab