Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is class? | Classes
Java Extended

What is class?What is class?

Class

A class is a fundamental concept in OOP programming. It refers to a template for creating objects. A class consists of fields (data) and methods (behavior). Let's consider the class Dog, where the fields (data) would be information about the dog's name and age, and the method (behavior) would make the dog introduce itself and say its name:

java

Dog.java

Let's consider what constitutes data (fields) and what represents behavior (methods):

As we can see from the diagram, we have fields that are not initialized within the class itself, as well as a method that is not yet called anywhere. Let's create an object of the Dog class in the main class and initialize its fields:

java

Main.java

We created an object of the Dog class and named it dog. The syntax for creating an object of a class is: ClassName objectName = new ClassName(); We also initialized the properties of the object by assigning values to the fields. Our dog's name is Brian, and the age is 13. The syntax for initializing the fields of an object is: objectName.fieldName = value; So now we have an object of the Dog class with initialized fields. Let's now invoke a method from our Dog class:

java

Main.java

We successfully invoked a method from the Dog class by using that method on the Dog object. You may have noticed the same syntax when we called methods from the String class earlier. Yes, String is also written by someone. It has fields and methods that we use effectively. Now it's your turn to write your own classes!

Note

A new class should always be located outside the body of another class or method. In other words, the class itself is the container for other fields and methods. Remember this to avoid syntax errors.

1. How to declare a class?
2. Should we create a new class inside another class?

How to declare a class?

Selecciona la respuesta correcta

Should we create a new class inside another class?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 4. Capítulo 1

What is class?What is class?

Class

A class is a fundamental concept in OOP programming. It refers to a template for creating objects. A class consists of fields (data) and methods (behavior). Let's consider the class Dog, where the fields (data) would be information about the dog's name and age, and the method (behavior) would make the dog introduce itself and say its name:

java

Dog.java

Let's consider what constitutes data (fields) and what represents behavior (methods):

As we can see from the diagram, we have fields that are not initialized within the class itself, as well as a method that is not yet called anywhere. Let's create an object of the Dog class in the main class and initialize its fields:

java

Main.java

We created an object of the Dog class and named it dog. The syntax for creating an object of a class is: ClassName objectName = new ClassName(); We also initialized the properties of the object by assigning values to the fields. Our dog's name is Brian, and the age is 13. The syntax for initializing the fields of an object is: objectName.fieldName = value; So now we have an object of the Dog class with initialized fields. Let's now invoke a method from our Dog class:

java

Main.java

We successfully invoked a method from the Dog class by using that method on the Dog object. You may have noticed the same syntax when we called methods from the String class earlier. Yes, String is also written by someone. It has fields and methods that we use effectively. Now it's your turn to write your own classes!

Note

A new class should always be located outside the body of another class or method. In other words, the class itself is the container for other fields and methods. Remember this to avoid syntax errors.

1. How to declare a class?
2. Should we create a new class inside another class?

How to declare a class?

Selecciona la respuesta correcta

Should we create a new class inside another class?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 4. Capítulo 1
some-alt