Java Classes/Objects

Farhad Valizada
3 min readNov 9, 2023

--

Java is an object-oriented programming language. That is, we are introducing Object-oriented programming. So what is OOP? As the name suggests, objects are used here. But what is an Object?

We can think of everything we see around us as an object. They are all an object in themselves. Be it animate or inanimate. Each object has a name, parameters, behaviors. Let’s explain them through the Dog object.

The identity of the Dog object refers to the name Dog. Dog object parameters include name, age, color, etc. Dog object behavior means eating, running, etc.

So what is Class? First, let’s look at the definition of a class.

A class is an object consisting of a constructor, parameters, and methods. If we look at the definition, we will see that the class is the object itself.

But what is a constructor? A constructor is the first instance of a class. A constructor can be viewed as a method, but it is not a method. The name of the constructor must be the same as the name of the class and doesn’t return a value. Let’s create a class and explain what we are talking about.

// class
public class Dog {
//parametr
public String name;
public String breed;
public int age;
public String color;

//constructor
public Dog() {
}

// method
public void run() {
System.out.println("Dog running");
}
}

You can create any number of parameters, constructors and methods in a class. The names of the created constructors are always the same, but the parameters they take must be different.

//constructor
public Dog() {
}

public Dog(int age) {
this.age = age;
}

public Dog(String breed) {
this.breed = breed;
}

public Dog(String name, String breed, int age, String color) {
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}

Parameters of a class are global variables. However, variables defined in methods and constructors are local variables. In the example above, the word this is used to assign the value of the variable entered from the constructor to the global variable. That is, by saying this.age, we are saying that the age variable is a global variable of the class.

But how can we use the class we created? Since a class is an object, in order to use it, we must create its object. To create an object of each variable, you need to use the word new. Now, let’s call the Dog class we created in our Main class and run the run method.

public class Main {
public static void main(String[] args) {
Dog dog=new Dog();
dog.run();
}
}

We have created the “dog” object here by calling “new Dog()”. By saying “dog.run()” we run the “run” method of the “dog” object. By saying “new Dog()” we call the constructor without parameters of the “Dog” class. If we want to call other constructors then we can write as :

Dog dog=new Dog(23);
Dog dog1=new Dog("dog 2");

Note that we may not create a default empty constructor when creating a class. Java itself does this for us. Note that a class can be public or default. According to Java’s standards, the class name must start with an uppercase letter.

--

--