course content

Course Content

Introduction to Java

ClassesClasses

The most important concept of Java is Java classes. A Java program is a collection of Java classes.

A class is a template for creating objects. A class defines the fields (data) and methods (behavior) that objects of that class will have.

Declaring a Class

The first thing you need to know about classes is how to declare a class.

We use the class keyword followed by the class name to declare a class. Methods and fields go inside the curly brackets {}.

java

Main.java

Java class names follow the same naming rules as variables. Specifically, a class name must start with a letter, an underscore (_), or a dollar sign ($). Any combination of letters, digits, underscores, or dollar signs can then follow it.

However, starting class names with capital letters is highly recommended.

Add fields and methods to the class

Once we declare the classes, we can define fields and methods related to that class. Let's say our person should have a name and age. And he should be able to say Hello.

java

Main.java

As you can see, we define name and age as variables, as we have done so far. Then we defined a method where the person can tell his name and age.

Note that this is only a class which is a template for creating an object. You are giving the name and age to a person only when you create the object or after creating the object.

Creating object

Next, you need to know how to create objects from the classes. To do that, Java has a keyword called new. Overall creating an object comes with two steps.

First, you define a variable of the class type you will use for creating the object. This variable will be used to reference the object.

java

Main.java

Create a new instance of the class using the new operator. This will allocate memory for the object.

java

Main.java

You can do both of these steps in one line.

java

Main.java

You can create as many objects as you want from one class.

java

Main.java

How to access class properties from the object

We have created our object person. But this person doesn't have a name or age. We can assign properties to an object using the following syntax:

Let's assign a name and age to our person.

java

Main.java

We can use a similar syntax to call the sayHello method from the person object.

java

Main.java

Let's see our complete code now!

java

Main.java

You can save this code in a file called Main.java and run it.

Note

  • As you can see, we created our object in the main method. That’s because Java starts executing the code from the main method. It’s okay to create objects anywhere else, but make sure Java can reach them starting from the main method;
  • Our Main class is public. We didn’t use such a word for the Person class. You will learn about it in the access modifier article. You must know that you can only have one public class in one Java source file. And the name of the java source file should be that class name.

I hope you now know the basics of Java Classes.

1. Which of the following is correct?
2. Which of the following is the correct syntax for creating an object of class Product?

question-icon

Which of the following is correct?

Select the correct answer

question-icon

Which of the following is the correct syntax for creating an object of class Product?

Select the correct answer

Section 2.

Chapter 2