Before I can explain the different programming concepts in Java, let’s take a look at one of the basic building block in Java - Class. I will not go in depth in this tutorial but I need to make sure that you can follow along in the next tutorial.
Assuming that you followed the last post, let’s start by creating a new package in our Helpful Java project. To create a package right click on the src folder -> New-> Package, I will call this package tutorial2.
Now let’s create a class in this package, right click on the tutorial2 package -> New -> Class and name it Person ->Finish
Eclipse will create a new class called Person. So what is a class? A class can be anything that has some attributes or properties. It is the most basic building block in Java. Anything you want to do in Java will require a class.
The attributes or properties of a class are known as member variables or fields of a class. They may be also called data of a class. For our Person class, we can have the following fields: a name, date of birth, gender, height, weight, etc. For now let’s keep it simple with just name.
The fields in the class by itself are not of much use, we need to be able to perform operations on this data. More importantly we need to access the fields. For these reasons we create methods that allow us to manipulate and perform operations on the data. Methods are also known as functions.
Eclipse allows us to create some basic methods easily by using Getters & Setters. Click Source -> Generate Getters and Setters using Fields
Check name -> Click OK
You should see 2 methods created for you: setName() and getName()
This method will set the value for the class field name. Public means that this method can be accessed from outside of this class. Void means that this method doesn’t return or send back anything to the caller. setName is the name of the method and String name is a parameter taken by this method. String is the datatype of the parameter “name” and “name” is the name of the parameter. The name of the parameter can be anything you want.
this.name = name;
Now this statement can get a little confusing so let’s get to it. This keyword refers to the current object or instance of the class. I have explained what an object is just down below.
Let’s try changing the parameter name to simplify things. Double click on the parameter name -> right click -> Refactor -> Rename… -> Type myName and hit enter.
Now we see
public String getName()
The getName() method returns or sends back a String type variable. As we can see the setName() had void whereas getName() has String.
This is the statement used to return something back to the caller. ’Return’ is a keyword in Java.
To use this class we will have to create an object of this class. Let’s first create a Junit test and in the test we will create the object.
Delete the fail("Not yet implemented"); line and type
Person farvin = new Person();
This is the syntax for creating an object of a class. Person is the class that we are using to create the object from. Farvin is the name of the object, (Farvin is my name, feel free to use your name or any name you want). The ‘new’ keyword is used to create an object. Person() is a constructor, we will look at constructors in a future tutorial. An object is an instance of a class. Objects allow us to access the data and methods of a class. The naming convention for objects is to use camelCase - start with lowercase and capitalise the next word if it’s a multi word name, for example “numberOfDays”.
Now we can use the setName() method. Type the following on the next line.
assertEquals("Name doesn't match", "Farvin Gonsalves", farvin.getName());
Save and let’s run this test, Right click -> Run As -> Junit Test
We have a green bar, this means that the test passed and we have verified that setName() works as it’s supposed to.
I know this is a lot to take in but these are the building blocks of Java, if you have any questions then please comment below, also if you liked this post then feel free to share it. I’m interested in continuously improving myself so if you have any feedback let me know below. Thank you for reading.