Making the beginning is the one third of the work — Object Oriented Programming.

Thukaraka
5 min readDec 31, 2020

Part-1

Hi friends! I am Thukaraka Pakeerathan currently studying the third year of Computer Science and Engineering at the University of Moratuwa. In my experience, we are coming with different backgrounds and different comfort levels with programming and computer science. If you’ve had more experience you will be able to work faster and grab things faster. Others you need a little more support, that’s also fine. More paths will support you. Normally we consider the Object-Oriented Programming-Java as the gateway module to the Computer Science and Engineering Department.

In this article Let me share some Keynotes I have learned.

The main method in Java

public static void main(String[] args)

This method is usually public and can be defined in any class. When a class has a main method, it can “run”, and Java will begin execution with the first line of the main method in the class and also we can have the main method in more than one class. Suppose consider if java class1 and java class 2 have main methods. Java class1 will run the main method in class1 and not the main method in class2 and java class 2 is also the same as java class1.

What does the “static” mean in the main’s signature?

The keyword “static”, if this is applied to a method or member variable it means it is defined for a class not for the objects of the class. The main is a general method. There are no calling objects. If you need to call any instance method first you need to create objects and then call the instance methods of those objects. Whatever other static methods can be called directly.

Why do we want to use classes and objects when we write our programs?

To motivate the use of classes and objects let me take an example problem as car factory. We can think of lots of real-world objects and entities that are part of this problem. Classes are custom data types that define to match the problem that we need to solve. Classes are kind of like a factory. Objects are things that come out of the factory.

A car factory knows how to produce a car. At first, sight when the cars are out of the factory they look the same but they may be different in there features depending on the need. They can be customized and changed without affecting all other cars. The same thing is going to happen for classes and objects.

Let’s take an example I am going to represent my location right now. So let’s define a class that represents the concept of location.

public class MyLocation
{
public double lat;
public double lon;

public MyLocation(double latIn, double lonIn) {
this.lat = latIn;
this.lon = lonIn;
}

}

The keyword “class” tells Java that creating a new class and the keyword public tells that this class is public to the world. We will talk about the keyword “public” later. Inside the curly braces, the class is defined. This file should have the same name as the class name and that is if the class is public. It is just a rule Java enforces. As we all know to define a point at least we need two lines. Here to represent my location I have represented latitude and longitude using double type variables. These are called member variables.

public MyLocation(double latIn,   double lonIn)  {
this.lat = latIn;
this.lon = lonIn;
}

Then this is a special method constructor. It takes the values of those arguments that are passed in and store them away in those member variables. Depending on the need you may define other methods in the class.

Public vs Private

In the above sample code, I have declared two member variables to be public. Let us take a closer look at what that means.

public class LocationTester
{
public static void main(String[] args) {
MyLocation LOC1 =
new MyLocation(-32.9, -117.0);
MyLocation LOC2 =
new MyLocation(-12.0, -77.0);

LOC1.lat=-12.04;
}
}

It means that anyone has access to that member variable from any class. As you can see here inside the class LocationTester I can change the value of latitude inside the object stored in LOC1. Because the member variable in the MyLocation class is declared as public. Similarly, if the methods also declared as public they can also accessible from any class.
On the other hand, if we declare member variables as private it can only accessible within the class.

public class MyLocation
{
private double lat;
private double lon;

public MyLocation(double latIn, double lonIn) {
this.lat = latIn;
this.lon = lonIn;
}

How ever if you try to change of LOC1.lat=-12.04,an error will be caused.

It is always a best practice to make member variables private. Because you as class designer you need ultimate control over who gets to see and change the data that is stored in each object of the class created. Methods can either be private or public depending on the need.

Getters and Setters

If we make member variables of our class private nobody have access to them. In order to give a little more access we use getters and setters.

public class MyLocation
{
private double lat;
private double lon;

public MyLocation(double latIn, double lonIn) {
this.lat = latIn;
this.lon = lonIn;
}
public double getLat(){
return this.lat;
}
public void setLat(double lat){
this.lat=lat;
}
}

The getLat() method over here gets the value of the member variable lat which is private and returns to the outside world and the setLat(double lat) method takes in a value, lat, and changes the value of the private member variable passed in by the user. But let us say nobody wants to change the value of the member variable you declared as private just provide a getter not a setter inside the class.

Thank you for reading…

--

--

Thukaraka

Software Engineer@ SyscoLabs Sri Lanka| Undergraduate | Computer Science and Engineering | University Of Moratuwa