1.1 The structure of a Java class and source code file
1.1.1 Structure of a Java class
A class can define multiple components for example:
- Package statement
- Import statement
- Comments
- Class declarations and definitions
- Variables
- Methods
- Constructors
1.1.1.1 Package Statement
All Java classes are part of a package; if a class is not defined in a named package it becomes part of a default package, which doesn´t have a name. If a class includes a package statement, it must be the first statement in the class definition (it cannot appear within a class declaration or after the class declaration) and if present, it must appear exactly once in a class:
package certification; //should be the first statement in a class class Course{ }
1.1.1.2 Import Statement
Classes and interfaces from the same package can use each other without prefixing the package name. But to use a class or interface from another package, the import statement can be used:
package University; import certification.ExamQuestion; // import must be placed after the package class AnnualExam{ examQuestion eq; }
The import statement follows the package but precedes the class declaration.
1.1.1.3 Comments
The comments in Java code can be placed at multiple places in a class. To place multiline comments, start with /* and end with */ . End-of-line comments start with // and are placed at the end of a line of code. Comments can be placed before the package statement.
/** * @author JRamirez // first name initial + last name End-of-line within a multiline * @version 0.0.2 * * Class to store the details of a monument */ package uni; // package uni End-of-line comment class Monument { int startYear; }
1.1.1.4 Class Declaration
Components of a class declaration:
- Access modifiers
- Nonaccess 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, {}
Example:
public final class Runner extends Person implements Athlete {}
1.1.1.5 Compulsory and optional elements of a class:
Compulsory
- Keyword class
- Name of the class
- Class body, marked by the opening and closing curly braces, {}
Optional
- Keyword class Access modifier, such as public
- Nonaccess modifier, such as final
- Keyword extends together with the name of the base class
- Keyword implements together with the name of the interfaces being implemented
1.1.1.6 Class Definition
The use of a class is to specify the behavior and properties of an object using methods and variables. It is a design from which an object can be created.
1.1.2 Structure and components of a Java source code file
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)
1.1.2.1 Definition of interfaces in a Java source code file
An interface is a grouping of related methods and constants, but the methods in an
interface cannot define any implementation.
interface Controls { void changeChannel(int channelNumber); void increaseVolume(); void decreaseVolume(); }
1.1.2.2 Definition of single and multiple classes in a single java source code file
A single class or interface can be defined in a single Java source code or multiple classes and interfaces can be within the same source code. The classes and interfaces can be defined in any order of occurrence in a Java source code file. If a public class or interface is defined, its name should match the name of the Java source code file.
1.1.2.3 Application of package and import statements in Java source code files
When an import or package statement is used within a java source code file, it applies to all classes and interfaces defined in that code.
1.2 Executable Java applications
1.2.1 Executable Java classes versus nonexecutable Java clases
An executable Java class is a class which, when handed over to the Java Virtual Machine, starts its execution at a particular point in the class in the main method. A nonexecutable class doesn’t have it. The programmer designates an executable class from all of the files of an application.
1.2.2 Main method
The first requirement in creating an executable Java application is to create a class with a method whose signature (name and method arguments) match the main method.
public class HelloExam { public static void main(String args[]) { System.out.println("Hello exam"); } }
This main method should comply with the following rules:
- The method must be marked as a public method.
- The method must be marked as a static method.
- The name of the method must be main.
- The return type of this method must be void.
- The method must accept a method argument of a String array or a variable argument of type String
The keywords public and static can be interchanged:
public static void main(String[] args) static public void main(String[] args)
1.3 Java Packages
1.3.1 The need for packages
Packages are used to group classes and interfaces, they also provide protection and namespace management. Subpackages can also be created within the packages.
1.3.2 Defining classes in a package using the package statement
//The first statement in a class or interface must be the package statement: package certification; class ExamQuestion { //..code }
Rules about packages:
- Per Java naming conventions, package names should all be in lowercase.
- The package and subpackage names are separated using a dot (.).
- Package names follow the rules defined for valid identifiers in Java.
- For packaged classes and interfaces, the package statement is the first statement in a Java source file (a .java file). The exception is that comments can appear before or after a package statement.
- There can be a maximum of one package statement per Java source code file (.java file).
- All the classes and interfaces defined in a Java source code file will be defined in the same package. There is no way to package classes and interfaces defined within the same Java source code file in different packages.
1.3.2.1 Directory Structure and Package Hierarchy
The hierarchy of the packaged classes and interfaces should match the hierarchy of the directories in which those classes are defined.
1.3.3 Using simple names with import statements
For using classes and interfaces in other classes of your code, there are two options, using the fully qualified name or using the import statement with a simple name of the class or package.
package office; class Cubicle { home.LivingRoom livingRoom; } package office; import home.LivingRoom; class Cubicle { LivingRoom livingRoom; }
1.3.4 Using packaged classes without using the import statement
By using its fully qualified name, a class or interface can be used without an import statement.
class AnnualExam { certification.ExamQuestion eq; }
This is often used when there are multiple classes and interfaces with the same name, because the import statement cannot be used in that case.
class AnnualExam { java.util.Date date1; java.sql.Date date2; }
1.3.5 Importing a single or all members of a package
By using an asterisk, all the public classes, members and interfaces of a package can be imported at once.
import certification.*; class AnnualExam { ExamQuestion eq; MultipleChoice mc; }
Importing a class in Java doesn’t add to the size of the file.
1.3.6 Importing Recursively
By using an asterisk, classes from a subpackage are not imported, only classes from the main package.
1.3.7 Default Package Import
If no explicit package is defined for classes or interfaces, they are imported in the default package automatically in all the classes and interfaces in the same directory.
class Person { // code } class Office { Person p; }
A class from a default package can’t be used in any named packaged class.
1.3.8 Static Imports
To import an individual static member of a class or all its static members, the import static statement must be used.
package certification; public class ExamQuestion { static public int marks; public static void print() { System.out.println(100); } } package university; import static certification.ExamQuestion.marks; class AnnualExam { AnnualExam() { marks = 20; } } //Importing all of the static members: package university; import static certification.ExamQuestion.*; class AnnualExam { AnnualExam() { marks = 20; print(); } }
1.4 Java Access Modifiers
1.4.1 Access Modifiers
Access modifiers control the accessibility of a class or interface and its members, by other classes and interfaces.
They can be applied to classes, interfaces, and their members (instance and class variables and methods). Local variables and method parameters can’t be defined using access modifiers.
Java defines four access modifiers:
- public(least restrictive)
- protected
- default
- private(most restrictive)
1.4.3 Public Access Modifier
Classes and interfaces defined using the public access modifier are accessible across all packages, from derived to unrelated classes.
1.4.4 Protected Access Modifier
Classes and interfaces defined using the protected access modifier are accessible to classes and interfaces in the same package and all derived classes even in separate packages. They cannot be accessed by unrelated classes in other packages.
1.4.5 Default Access (package access)
Classes and interfaces defined without any explicit access modifier are defined with package accessibility (default accessibility). They can only be accessed by classes and interfaces defined in the same package.
1.4.6 Private Access Modifier
The members of a class defined using the private access modifier are accessible only to themselves. Private members are not accessible outside the class they are defined.
1.5 Nonaccess Modifiers
1.5.1 Abstract Modifier
When added to the definition of a class, interface, or method, the abstract modifier changes its default behavior.
1.5.1.1 Abstract Class
When the abstract keyword is prefixed to the definition of a concrete class, it changes it to an abstract class. An abstract class can’t be instantiated. An abstract class can be defined without any abstract methods but a concrete class cannot define an abstract method.
1.5.1.2 Abstract Interface
An interface is an abstract entity by default. The Java compiler automatically adds the keyword abstract to the definition of an interface.
1.5.1.3 Abstract Method
An abstract method doesn’t have a body. Usually, an abstract method is implemented by a derived class.
1.5.1.4 Abstract Variables
No type of variable can be defined as abstract.
1.5.2 Final Modifier
The keyword final changes the default behavior of a class, variable, or method.
1.5.2.1 Final Class
A class defined final cannot be extended by other classes.
1.5.2.2 Final Interface
No interface can be marked as final.
1.5.2.3 Final Variable
A final variable can only be assigned a value once.
1.5.2.4 Final Method
A final method defined in a base class cannot be overridden in a derived class.
1.5.3 Static Modifier
Can be applied to the definitions of variables, methods, classes, and interfaces.
1.5.3.1 Static Variables
They are common to all instances of a class and are not unique. They are shared by all the objects of the class. Static variables may be accessed even when no instances of a class have been created.
1.5.3.2 Static Methods
Static methods aren’t associated with objects and can’t use any of the instance variables of a class. They can be used to use or manipulate static variables of a class.
Nonprivate static variables and methods can be inherited by derived classes and can be redefined within the derived class.
1.5.3.3 What can a static method access?
Non-static variables and methods can access static variables and methods. Static methods and variables cannot access the instance methods of a class.
This entry is the first part of of a series on Java Basics, for further reading:
Java Basics II
Java Basics III
Filed under: Java, Programming