|
| 1 | +// Java Fundamentals 07 - Classes, methods, fields, access modifiers |
| 2 | +package com.fledger.javafundamentals.jf07classes; |
| 3 | + |
| 4 | +import java.util.Date; |
| 5 | + |
| 6 | +// From now on we will have a separate package per module, as we may use multiple files for each module. |
| 7 | +// All files for this module are inside the com.fledger.javafundamentals.jf07classes package. |
| 8 | +// Java is an object oriented language. Objects are elements containing data, in the form of fields, |
| 9 | +// and code, in the form of functions. In Object Oriented (OO) lingo, object fields are known as attributes |
| 10 | +// and object functions are known as methods. |
| 11 | +// We say that objects contain a state (given by the attributes) and a behavior (given by the methods). |
| 12 | +// An object is a computational model of an entity, like a specific car, or a specific alarm, etc. |
| 13 | +// A class is a definition, or a blueprint, for a type of entity, like "Car" (the idea of a car) or "Alarm" (same). |
| 14 | +// Objects are created based on a class. For example, in Java, in the following statement: |
| 15 | +// Alarm wakeUpAlarm = new Alarm(); |
| 16 | +// wakeUpAlarm is an object, or instance, of class Alarm. The class Alarm defines the fields and methods of all alarms. |
| 17 | + |
| 18 | +// The following statement and block (com.fledger.javafundamentals.jf07classes.JF07_Classes) define a class: |
| 19 | +public class JF07_Classes { |
| 20 | + // Everything between the { } following the class name define the content of the class. |
| 21 | + // As we said, a class can have data... inside a class, data members, or fields, are called fields or attributes: |
| 22 | + int anIntegerField; |
| 23 | + |
| 24 | + // A class can also have Java code within functions, also are called methods: |
| 25 | + void thisIsAMethod() { |
| 26 | + System.out.println("This is a method that returns nothing (void) and prints this message."); |
| 27 | + } |
| 28 | + |
| 29 | + // This class contains the entry point (function main) for our program (see JF01_TheBasics.java) |
| 30 | + public static void main(String[] args) { |
| 31 | + |
| 32 | + // A static method can call another static method. Static methods belong directly to the class (see BetterAlarm) |
| 33 | + printCurrentTime(); // prints current date and time |
| 34 | + // A class in java is a type, so you can declare variables of the class type (see Alarm.java in this package): |
| 35 | + Alarm wakeUpAlarm; |
| 36 | + // You can assign a value to a variable of the class type. The variable contains a reference to an object: |
| 37 | + wakeUpAlarm = new Alarm(); // the keyword "new", followed by a class type and (), creates a new object of that type; |
| 38 | + // You can access members (fields and methods) of an object by using a reference variable, dot (.), and member name: |
| 39 | + wakeUpAlarm.alarmMessage = "It is time to wake up... WAKE UP!!!"; |
| 40 | + // You can have fields that are also references to other objects: |
| 41 | + wakeUpAlarm.alarmDateAndTime = new Date(); // sets alarmDateAndTime of WakeUpAlarm to a new Date object set to now. |
| 42 | + // You can access members of these fields with the dot (.) notation: |
| 43 | + Long currentTime = wakeUpAlarm.alarmDateAndTime.getTime(); // get the time (now) from the Date object as a long. |
| 44 | + Long inThreeSeconds = currentTime + 3000L; // Adds 3 thousand milliseconds (5 seconds) to the current time (now). |
| 45 | + wakeUpAlarm.alarmDateAndTime.setTime(inThreeSeconds); |
| 46 | + while (true) { // forever loop. There are better ways to wait for a timed event, but this will do for now. |
| 47 | + Date now = new Date(); // create a new object with the current time; |
| 48 | + if (now.after(wakeUpAlarm.alarmDateAndTime)) { // if current time passed the time set in the alarm |
| 49 | + wakeUpAlarm.goOff(); // alarm goes off and prints message on the console... this is an object behavior (method) |
| 50 | + break; // if you use forever, you need to break out of loop under some condition (see JF06_ArraysLoops) |
| 51 | + } |
| 52 | + } |
| 53 | + printCurrentTime(); // should be 3 seconds after previous printed time. |
| 54 | + |
| 55 | + // One of the principles of object oriented programming is encapsulation or "data hiding". |
| 56 | + // You can hide the object fields from external access (hide the data, or encapsulate), |
| 57 | + // so only methods of the object can access them. This way you ensure no one else will corrupt your object data. |
| 58 | + // To do so we use access modifiers in the BetterAlarm.java file (look into that file): |
| 59 | + BetterAlarm anotherWakeUpAlarm = new BetterAlarm(); |
| 60 | + anotherWakeUpAlarm.setToGoOffInTheseManySeconds(1); |
| 61 | + anotherWakeUpAlarm.setMessage("Please, wake up. Better alarms are more polite."); |
| 62 | + for (; ; ) { // another way to write the forever loop |
| 63 | + Date now = new Date(); // create a new object with the current time; |
| 64 | + if (now.after(anotherWakeUpAlarm.getDateTime())) { // if current time passed the time set in the alarm |
| 65 | + anotherWakeUpAlarm.goOff(); // alarm goes off and prints message on the console... |
| 66 | + break; // get out of the "forever" loop (see JF06_ArraysLoops) |
| 67 | + } |
| 68 | + } |
| 69 | + printCurrentTime(); // should be one second after previous printed time. |
| 70 | + |
| 71 | + // BetterAlarm contains static members. You don't need to instantiate an object in order to access the static |
| 72 | + // member. You access it directly using the class name: |
| 73 | + BetterAlarm.setDefaultMessageForBetterAlarm("Better Alarm also asks you to wake up."); |
| 74 | + // Creates a betterAlarm object: |
| 75 | + BetterAlarm betterAlarm = new BetterAlarm(); |
| 76 | + // All you need to do to prepare the alarm now is call one method. |
| 77 | + betterAlarm.prepareAlarm(); // This is a package private method: only classes in this package can call it. |
| 78 | + // This is even another way to write the code to make the alarm go off, with only two lines of code: |
| 79 | + for (Date now = new Date(); now.before(betterAlarm.getDateTime()); now = new Date()) ; |
| 80 | + betterAlarm.goOff(); |
| 81 | + printCurrentTime(); // should be two seconds (default delay for BetterAlarm) after previous printed time. |
| 82 | + } |
| 83 | + |
| 84 | + private static void printCurrentTime() { |
| 85 | + Date now = new Date(); |
| 86 | + System.out.println("The date and time now is: " + now); |
| 87 | + } |
| 88 | +} |
0 commit comments