Chapter:Classes - Declaration

From Juneday education
Jump to: navigation, search

Meta information about this chapter (TODO)

Expand using link to the right to see the full content.

Introduction

There are many "chapters" about classes in this course material, this being the first one. The following chapters will focus on various parts of the class source code file and other aspects of classes in more detail, and this chapter and lecture should be seen as an introduction to the chapters which follow.

Purpose

Make the student prepared to what the next chapters on classes will look at, by giving a short overview of the various parts in a Java class declaration in a source code file.

Goal

After this chapter, the student shall be prepared for the next chapters on classes, having a basic idea of the various parts of a source code file declaring a class.

Instructions to the teacher

Common problems

Don't go into too much details when running through this lecture. It is meant as a preparation and motivation for the next chapters about classes. The next chapters will have enough details about various topics related to classes.

Full frontal - code up-front!

Here's an example of a class declaration. You are not expected to understand everything in this class, but we want to expose you to some code anyway, so that you are prepared for what to expect from the following chapters and lectures.

import java.time.LocalDate;
import java.time.Period;

/**
 * A class representing a Student.
 */
public class Student {

  private String name;
  private LocalDate birthDate;

  /**
   * Constructs a new Student.
   * @param name The name of the new Student
   * @param birthDate The birthDate of the new Student
   */
  public Student(String name, LocalDate birthDate) {
    this.name = name;
    this.birthDate = birthDate;
  }

  /**
   * Returns the name of this Student
   * @return The name of this Student
   */
  public String name() {
    return name;
  }

  /**
   * Returns the age of this Student
   * @return The age of this Student
   */
  public int age() {
    return Period.between(birthDate, LocalDate.now()).getYears();
  }

  /**
   * Returns a String representing this Student on the form<br>
   * Peter Parker (46 years old)
   * @return This student as a String summary
   */
  @Override
  public String toString() {
    return new StringBuilder(name)
      .append(" (")
      .append(age())
      .append(" years old)")
      .toString();
  }
}

And here's a class with a main method which shows how to use the Student class:

import java.time.LocalDate;

public class StudentDemo {

  public static void main(String[] args) {
    Student me = new Student("Rikard Fröberg", LocalDate.of(1971, 11, 24));
    System.out.println(me);
  }

}

Here's a test-run of the Demo:

$ javac StudentDemo.java && java StudentDemo
Rikard Fröberg (46 years old)
$

Introduction to class declaration

In the previous chapter we introduced objects and classes so we should all have a basic idea of what objects and classes are.

The chapters on classes deals with the components of a class and the source code layout for a class declaration. What parts constitutes a class? Before we answer that, we start by looking at what are the parts of a class as defined in a source code file. A Java class definition in a source code file can contain the following elements:

  • A package declaration - declares logical association expressed as a package (which corresponds to the path to the class file)
  • import statements (if needed)
  • The actual class declaration with the class name and the start of the block of code for the class
  • The variables needed to keep the state of objects created from this class (instance variables)
  • The variables common to all instances created from this class (static variables)
  • The constructor(s) providing means to create instances from this class
  • The methods of instances of this class - what objects of this class can do
  • The methods which exist independently from instances of this class (methods which don't involve instances of this class)
  • Nested and inner classes - a class definition can actually contain other classes - this is outside of the scope for this book
  • Initializers - you can actually put blocks of code directly in the class body - this is outside of the scope for this book

We'll discuss these components one by one, in the chapters on Classes. Note that the last two components are only included here in order for us to be more complete. We don't want to hide these components for you, but we will not go into any details about the use of them in the scope of this book.

Chapters about classes

Here are the chapters about classes in this book, so that you can keep check on your progress:

Links

Slides and videos

Java Classes Declaration intro (Full playlist) | Java - Classes - Declaration intro (video) | Classes - Declaration - Intro (pdf)

Further reading

Where to go next

Next page has the exercises for this chapter (if any): Classes_-_Declaration_-_Exercises

« PreviousBook TOCNext »