Java Basics in Java EE 7

The Structure of a Java Class and Source Code File
Difficulty
Intermediate
Duration
1h 58m
Students
8
Description

In this course, we will learn the concepts of Java EE 7 with a focus on Java Basics.

Learning Objectives

  • What Java classes, Executable Java, Access Modifiers are and how they work

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers

Prerequisites

  • Have at least 2 years of Java development experience 
Transcript

Hello there. In this section, we'll talk about some Java basics. These basics belong to Java SE. But why do we need to talk about them?

The reason is very simple. We refresh our Java knowledge and these are fundamentals of Java, so they are very important. So, what will we learn or, more truly, remember in this section? First, we'll examine how to define the structure of Java class. This part of the section contains structure of a Java class with its components, of course, package and import statements, class declarations, comments, variables, and methods, and the difference between the components of a Java class and that of a Java source code file. Then we'll examine how to create executable Java applications with a main method. This part of the section contains the right method signature for the main method to create and executable Java application and the arguments that are passed to the main method. Our third topic in this section is Java packages. This part of the section contains how important the other Java packages are. Get the right syntax and semantics to import classes from packages and interfaces in other classes.

Next chapter is applying access modifiers. This part of the section contains how to apply an access modifier to a class and its members, and how to determine the accessibility of code with these modifiers. This section covers the structure and components of both a Java source code file and a Java class. It also covers the differences between a Java source code file and a Java class. First things first. Start your exam preparation with a clear understanding of what is required from you in the certification exam. For example, try to answer the following query from a certification aspirant. I come across the term "Class" with different meanings. Class person, the Java source code file, Person.java and Java byte code stored in person.class. Which of these structures is on the exam? To answer this question, take a look at this figure which includes the class person, the files Person.java in person.class and the relationship between them. As you can see in the figure, a person can be defined as a class person.

This should reside in a Java source code file. Using this Java source code file, the Java compiler generates byte code compiled code for the Java virtual machine and stores it in person.class. The scope of this exam objective is limited to Java classes, class person, and Java source code files. The OCA Java SE 7 Programmer one exam will question you on your understanding of the structure and components of a Java class to find using the keyword class. A class can define multiple components. Of course, I know you're here for the Java EE 7 exam, but I believe this topic is really important and fundamental. So, in this section, we will refresh our Java knowledge. Let's continue. All the Java components that you've heard of can be defined within a Java class. Here's a quick list of the components of a class, which we'll discuss in detail in this section: the package statement, the import statement, comments, class declarations and definitions, variables, methods, constructors. All Java classes are part of a package. A Java class can be explicitly defined in a named package.

Otherwise, it becomes part of a default package which doesn't have a name. A package statement is used to explicitly define which package a class is in. If a class includes a package statement, it must be the first statement in the class definition. The package statement cannot appear within a class declaration or after the class declaration. The following code will fail to compile. The following code will also fail to compile because it places the package statement within the class definition. Also, if present, the package statement must appear exactly once in a class. This code won't compile. Classes and interfaces in the same package can use each other without prefixing their names with the package name. But to use a class or an interface from another package, you must use its fully qualified name because this can be tedious and make your code difficult to read. You can use the import statement to use the simple name of a class or interface in your code. Let's look at this using an example class, annual exam which is defined in the package university.

Class annual exam is associated with the class certification.examquestion. Here's the code for class annual exam. Note that the important statement follows the package statement but proceeds the class declaration. What happens at the class annual exam isn't defined in a package. Will there be any change in the code if the class annual exam and the exam question are related? In this case, the class annual exam isn't part of an explicit package, but the class exam question is part of package certification. Here's the code for class annual exam. As you can see, in the previous example code, the class annual exam doesn't define the package statement but it defines the import statement to import the class certification.examquestion. If a package statement is present in a class, the import statement must follow the package statement. It's important to maintain the order of the occurrence of the package in import statements. Reversing this order will result in your code failing to compile. We'll discuss import statements in detail in the next section.

You can also add comments to your Java code. Comments can appear at multiple places in a class. A comment can appear before and after a package statement, before and after the class definition, before and within and after a method definition. Comments come in two flavors: Multi-Line Comments and End-Of-Line Comments. Multi-line comments span multiple lines of code. They start with slash asterisk (/*) and end with asterisk slash (*/). Here's an example. Multiline comments can contain any special characters including Unicode characters. Here's an example. Most of the time, when you see a multiline comment in a Java source code file, .java file, you'll notice that it uses an asterisk to start the comment in the next line. Please note that this isn't required. It's done more for aesthetic reasons. End-of-line comments start with slash slash (//) and as evident by their name, they are placed at the end of a line of code. The text between // and the end of the line is treated as a comment which you would normally use to briefly describe the line of code. Here's an example.

In the earlier section on the package statement, you read that as a package statement, if present, should be the first line of code in a class. The only exception to this rule is the presence of comments. A comment can proceed a package statement. The following code defines a package statement with multiline and end-of-line comments. Line B defines an end-of-line code comment within multiple code. This is acceptable. The end-of-line code comment is treated as part of the multiline comment, not as a separate end-of-line comment. Line C and D define end-of-line code comments. Line E defines an end-of-line code comment at the start of a line after the class definition. The multiline comment is placed before the package statement which is acceptable because comments can appear anywhere in your code. The class declaration marks the start of a class. It can be as simple as the keyword class followed by the name of a class. Time to get more details.

The declaration of a class is composed of the following parts: access modifiers, non-access modifiers, class name, name of the base class if the class is extending another class, all implemented interfaces if the class is implementing any interfaces, class body, class fields, methods, constructors included within a pair of curly braces ({}). Let's look at the components of a class declaration using an example. The components of the preceding class can be pictorially depicted as shown in figure. Keyword class access modifier, such as public name of the class, non-access modifier, such as final class body marked by the opening keyword extends together with the name and closing curly braces of the base class keyword. Keyword implements together with the name of the interface is being implemented. A class is a design the properties and behavior of an object. 

The properties of an object are implemented using variables, and the behavior is implemented using methods. For example, consider a class as being like the design or specifications of a mobile phone, and a mobile phone as being the object of that design. The same design can be used to create multiple phones, just as the Java Virtual Machine, JVM, uses a class to create its objects. You can also consider a class being like a mold that you can use to create meaningful and useful objects. A class is a design from which an object can be created. Let's define a simple class to represent a mobile phone. Points to remember. A class name starts with the keyword class. Watch out for the case of the keyword class. Java is case-sensitive. dot class lowercase C isn't the same as class uppercase C. You can't use the word class capital C

to define a class. The state of a class is defined using attributes or instance variables. The behavior is defined using methods. The methods will include the argument list if any. Don't worry if you don't understand these methods. The methods are covered in detail later in this chapter. A class definition may also include comments and constructors. An important note here, a class is a design from which an object can be created. Revisit the previous example. Because the variables model, company, and to store the state of an object, also called an instance. They are called the instance variables or instance attributes. Each object has its own copy of the instance variables. If you change the value of an instance variable for an object, the value for the same named instance variable won't change for another object. The instance variables are defined within a class but outside all methods in a class. A single copy of a class variable or static variable is shared by all the objects of a class.

The static variables are covered with a detailed discussion of the non-access modifier static. The methods make call and receive call or instance methods which are generally used to manipulate the instance variables. A class method or static method is used to work with the static variables as discussed in detail in the next section. Class Phone defines a single constructor. A class constructor is used to create and initialize the objects of a class. A class can define multiple constructors that accept different sets of method parameters. A Java source code file is used to define classes and interfaces. All your Java code should be defined in Java source code files, text files whose names end with .java. The exam covers the following aspects of the structure of a Java source code file: definition of a class and an interface in a Java source code file, definition of single or multiple cases and interfaces within the same Java source code file. Application of import and package statements to all the classes in a Java source code file. We've already covered the detailed structure and definition of classes.

Let's get started with the definition of an interface. An interface is a grouping of related methods and constants, but the methods in an interface cannot define any implementation. An interface specifies a contract for the classes to implement. Here's a quick example to help you understand the essence of interfaces. No matter which brand of television each one of us has, every television provides the common functionality of changing the channel and adjusting the volume. You can compare the controls of a television set to an interface and the design of a television set to a class that implements the interface controls. Let's define this interface. The definition of an interface starts with the keyword interface. An interface can define constants and methods. Remember, Java is case-sensitive. So, you can't use the word interface with a capital I to define an interface. You can define either a single class or an interface in a single Java source code file or many such files.

Let's start with a simple example. A Java source code file called single class dot java. That defines a single class, Single Class. There is no required order for the multiple classes or interfaces that can be defined in a single Java source code file. If you define a public class or an interface in a class, its name should match the name of the Java source code file. Also, a source code file can't define more than one public class or interface. If you try to do so, your code won't compile. So, that's it. In this section, we talked about the structure of Java class and source code file. Hope to see you in our next lesson. Have a nice day.

About the Author
Students
1362
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics