Structure of a Java class

A class in Java is defined by the class keyword and for beginners, to get a better understanding, in this tutorial I will present the basic knowledge about the structure and its components in possible detail

First of all, let’s say a Java class consists of the following components:

  • The package statement declares where our class will be stored in the whole project.
  • The command to import other classes will be used in our class.
  • Comments.
  • Declare and define our class.
  • Variables.
  • Methods.
    The constructors.

Now we will go into detail about each of these components.


Package command

All classes in Java must be in a package, we can name the package or else our class will be in the default package. To declare a package for a class, we will use the package statement followed by the package name.

Its syntax is as follows:

with com.huongdanjava is the name of the package.

For example, I define Student class in the package com.huongdanjava as follows:

Structure of a Java class

The package statement cannot be inside or behind the class definition, if you put it inside or behind the definition of the class, the code of the class will be compiled immediately.

For example:

Structure of a Java class

Or

Structure of a Java class

In a class, we can not declare more than once the package statement, if you try to declare, then a compile error will occur.

Structure of a Java class


Import command

The import statement is used to declare the classes to use in our class. If the classes we use are in the same package as our class, we may not need to declare the import statement for these classes.

Its syntax is as follows:

or

For example, we have two classes, Student and Class, in the com.huongdanjava package.

Structure of a Java class

At this point, if you use the Student class in the Class class, you do not need to use the import statement to declare the Student class.

Structure of a Java class

If now the Student class is moved to the com.huongdanjava.other package

Structure of a Java class

It is imperative that I have to declare the import statement for the Student class

Structure of a Java class

The import statement must be after the package statement; if you do something wrong, the compile error will occur.

Structure of a Java class


Comments

Java allows us to use a comment in the code. Comment can appear anywhere in the Java code: before or after the package statement, and of course the import statement, before and after the definition of the class, before and after the definition of the method.

There are two ways to add a comment to Java code:

  • Multi-line comment: begin with /* and end with */, all characters between these two characters are considered commented.
  • Comment at the end of the line: begin with //, all the characters after that are considered comments.

Take the following example:

Structure of a Java class

This is a multi-line comment. Normally when using multi-line comments, we add the * at the beginning of each line as follows:

Structure of a Java class

This is not required, only adds to the aesthetics of our code only.

Also below is one line comment:

Structure of a Java class


Class declaration

In the simplest way, a class must be declared using the class keyword, followed by the name of the class and two curly braces.

For example:

Structure of a Java class

In addition, to declare a class we can also add other components such as:

  • Access modifier.
  • Nonaccess modifier.
  • The name of the parent class if our class extends from another class.
  • Class interfaces if our class implements one or more interfaces.
  • Body of the class

For example:

Structure of a Java class


Definition of class

A class defines the properties and behavior of an object. In a class, the variables represent the properties of the object and the methods that represent the behavior of the object.

For example, a student whose attributes are the student’s name and age, the student’s behavior is school. To display this student with a Java object, we will declare the student’s name and age using variables, and daily attendance will be the method. As follows:

Structure of a Java class


Variables

A class will contain many variables that represent the properties of an object.

As the example above, variable name and age are the properties of the Student object.

Methods

Similar to a variable, but the method is to show the behavior of an object.

Constructors

Each Java object must have at least one constructor, because without the constructor we will not be able to create the Java object.

We can have many constructors in a class, but they must be different.

3.8/5 - (5 votes)

Add Comment