Chapter:Classes - Using your classes

From Juneday education
Jump to: navigation, search

Meta information about this chapter

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

Introduction

This chapter fills, in the authors' opinion, a void found in most books and courses the authors are aware of. A typical Java Book (and/or course) starts with the basics (primitive types, expressions, operators, control flow - if, for, while etc - to move on to more object oriented concepts such as objects, methods, classes etc. After showing how to write a class very thoroughly, the reader/student knows how to write a class. But what we feel often is missing, is the rationale for actually going about to write your own class! The why seems to be missing. And the how in terms of putting the class you wrote to use, seems also often be missing, in the authors' opinion.

Therefore, the purpose of this chapter is to give a reason for writing classes, and a use for the written classes, to the readers/students.

We hope that this chapter will serve as a motivation for writing custom classes in that it shows that the classes you write will then be used (by you or your colleagues).

Purpose

See Introduction above!

Goal

After this chapter, the student shall understand:

  • Why they learned how to write their own classes
  • That writing your own classes actually helps you write an application or system in Java
  • Most classes needed to model stuff in your typical application is not included in the Java standard API - you need to write them yourself!

Instructions to the teacher

Common problems

In our experience, students have a hard time to understand why and when to write classes. Sometimes they write code which actually does something (using other things which seem to just be "there for them"), a typical example would be the main method. Code that drives the application execution forward in a clear way. Then, in the middle of a course, they are told to stop writing such code, and focus on stuff which more describes stuff, than actually does stuff which clearly drives the execution forward.

Try to convey the idea that the Java API which comes with the JDK installation isn't complete - There's no way that Sun Microsystems or Oracle would predict that you would need a Passport class or a Student class for your application, when they wrote the standard API classes. Most of the stuff you want to model in your applications is stuff you will have to describe yourself and write classes for yourself. This burden pays off, since if you write classes which well models the objects in the real world your application is using, then those classes and objects will greatly simplify the logic code in the application (which will use objects created from your own classes).

It should be helpful if the students understand that putting some time on writing classes from which they later can create objects, will actually greatly simplify the writing of the more "action oriented" code that actually drives the execution of the program forward.

We think it is good and helpful to make the students think of two different roles of Java programmers

  • API (or service) programmers, who write code for there colleagues and others - code which typically describes something and its behavior
  • Client programmers who write applications that use the API programmers' code.

Sometimes you focus on the logic of the application at hand, and sometimes you put on a different hat and write code which serves as useful tools for other programmers (or yourself at a later point in time).

Classes: Using our classes

Now that we have seen the rules on how to write classes with variables, constructors and methods, we'll look a little at how we use these classes in programs which actually do something. How do we write programs using our own classes?

  • Typically there is only one main in a separate class (not counting tests).
  • typically the Main class (class with the main) is in a separate package
    • typically you need a package statement here too because of that
    • typically you need import statements in the Main class (also because of that)
  • Other normal classes (classes we write that don't have the main method) often also uses other classes
    • If we write a class, it probably needs more classes which we also write ourselves
    • Related classes are in the same package and can "use" eachother without import (otherwise we need that)
    • One use can be as a return type for a method
    • Another use can be as the type of an instance variable

We don't expect you to learn that list of hints just by reading. So head on to the exercise chapter after seeing the videos!

Chapters about classes

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

Links

Lecture slides and videos etc

Further reading

  • TODO: Find more resources which focus on actual Java programming where one uses one's own classes and objects...

Where to go next

Next page has exercises for this chapter: Classes_-_Using_your_classes_-_Exercises

« PreviousBook TOCNext »