Java Class Structure

In Java, the basic building blocks are the Classes, when you define a class you will describe all the parts and characteristics of one of those building blocks. The use of the classes is to instantiate/create Objects. An object is a runtime instance of a class in memory.

An object is often referred to as an instance since represents a single representation of the class. All the objects of different classes represent the state of your program. The first that we need to do is understand is the Java Class Structure because as we mention we create objects using classes.

Java basic Class

To create a basic class we use the modifier access (we can review it later in the access modifiers) keyword class followed by the name of the class.

[access modifier] class ClassName { 
... }

For example:

/**
* The class describes the java basic structure
*/
public class MyClass {
//some methods
//Some attributes
}

Let’s explain the sintaxis.

  • Access modifier. In Java, the access modifier determines the visibility and accessibility of the class. For a basic class (like in the example) we use the access modifier with the keyword public but also can be used as the default (to use the default keyword we only need to not specify an access modifier, but this has some rules to follow, so for the moment leave it as public).
  • class keyword, this keyword is used to indicate to the compiler that we are creating a class.
  • Name of the Class. This is the name given to your class. The name must start with an Upper letter and should follow the camelCase convention.

Java Class Name rules

Besides the use of the camelCase convention, we have more rules about the naming of a class. let’s review all those rules.

  1. The name must start with a Upper letter or underscore “_” (Since Java 9 the underscore character “_” is not longer recommended for use as an identifier in Java, because the underscore was introduced as a reserved keyword to be used in future enhancements of the language, avoid to use it).
  2. After the first character, you can use any letter, number, and underscore character in any combination.
  3. There are not allowed any white spaces or special characters, except the underscore character.
  4. Use the camelcase convention. you can read about it here.

Here are some examples of valid class names in Java.

public class MyClass {
// Class content
}
public class My_Class {
// Class content
}
public class MyClass123 {
// Class content
}
public class MyClass_123 {
// Class content
}

Here also are some examples of invalid class names in Java.

public class 123Class { // Number in the beginning in the class name
// Class content
}
public class My Class { // White Space in the name
// Class content
}
public class My-Class { // Special character in the class name
// Class content
}

We just review the Java basic structure of a class, let’s continue with the elements that a class can have too.

Class variables and methods

The classes also can have two primary elements: fields and methods, in other languages the fields are called variables, and the methods are often called functions or procedures. In OOP terminology these are called properties and behaviors. (We will review more in detail in the next topics).

The below class has field and two methods.

/**
* The class describes the java basic structure, with a Variable and two methods
*/
public class Animal {
//variable
String name;
//method
public String getName(){
return name;
}
//method
public void setName(String newName){
name = newName;
}
}

The line 7, we define a variable named name. In this variable we define the type of that variable a String. For now, you just need to know that a variable of type string stores text, like "Hello, I am a Squirrel".

On lines 10-12, we define our first method called getName(). A method is code block that performs a specific action that can be called when that action is needed. We use again the keyword public, because this method may can be called from other classes. Next is the return type of the method, in this case, the method returns an String.

Also is possible that our method don’t need to return nothing, so we will use the keyword void, like the method setName() on lines 15-17, with this keyword we indicate the method will not return anything at all. This method also requires information to be supplied to it from the calling method; this information is called parameter. This means the caller should pass in one String parameter and expect nothing to be returned.

In the methods we have two pieces that are considered special, these are the method name and the parameters that are called Method Signature that identifies the method as unique.

Class comments

Another element in the classes are the comments, the comments aren’t executable code, so you can place them in many places in the class.

In Java, there are three types of comments: single-line, multiple-line, and Javadoc comments.

Single-line comment

This comment begins with two slashes “//“, the Java compiler ignores anything you type after these characters appears on the same line.

// comment until end of line

Multiple-line comment

This comment is also called multiline comment, this comment includes anything that comes from the symbol “/*” until the compiler finds the symbol “*/“, the people usually types an “*” at the beginning of each line of a multiple-line comment to make it easier to read, but you don’t have to.

/* Multiple
* line comment
*/

/* Multiple
line comment
*/

Javadoc Comment

This comment is similar to a multiple-line comment ignore all between the multiple-line comment, except it starts with “/**“. This symbol also tells to the Javadoc tool to pay special attention to the comment, because it will be used to document the class and the methods.

/**
* Javadoc Multiple-line comment
* @author Cesar Pasillas
*/

The below class shows an example of the usage of the comments in a Java Class:

/**
* Javadoc Multiple-line comment
* This class shows the usage of the comments in java
* Single-line, Multiple-line, and Javadoc comments.
* @author Cesar Pasillas
*/
public class AnimalWithComments {
//This is a single line comment, we use to comment an step or a line of code
String name;
/**
* Javadoc Multiple-line comment
* This method is to return the current Animal name
* @author Cesar Pasillas
*/
public String getName(){
return name;
}
/**
* Javadoc Multiple-line comment
* This method is to setor update the name for the Animal.
* @author Cesar Pasillas
*/
public void setName(String newName){
name = newName;
}
/*
* This is multiple-line comment
* I comment these methods because is not longer needed util we add the field age of the Animal
public void setAge(int newAge){
age = newAge;
}
public void getAge(){
return age;
}
*/
}

In this lecture, we reviewed the basic Class structure in Java. The most simple class is a class without any modifiers, fields or methods, commonly referred to as an “empty” class. However, in a Java Class, we primarily will focus on two essential elements: Methods and Fields. These elements allow us to provide functionality and define the attributes of our objects. Additionally, comments play a crucial role in describing the functionality of methods or the class itself, making them valuable for understanding and maintaining the code.

I will see you in the next lecture the main() method.

Happy Learning!!!

Leave a comment