Object and Class


Objects are key to understanding OOP.
There are many examples of real world objects, such as: your cup, your computer, you.
Real world objects have 3 characteristics.

1. property

Each object has properties.
You have properties, e.g., name, mobile, waist circumference.

2. behavior

You have behaviors, e.g., run, walk.

3. one

Every object are only one.
You are only one.

Software objects

Software objects are similar to real world objects.

Software objects are a combination of data and functions.

A property of the real world object is mapped to the data,
A behavior of the real world object is mapped to the function.


A class is the Casting mold from which individual objects are created.
To create an object in a Java program, you must first create a class.

How to make a class

Let's think that we have to develop a program for student attendance management.
We are able to extract the properties and behaviors of a student, as follows.

property: name, total number of absence days.
behavior: absent

Now we can create a student class.

class Student {
	String name;
	int totalAbsenceDays;
	void absent() {
		totalAbsenceDays = totalAbsenceDays + 1;
class Student {..}
The class name comes after the class keyword.
File name is recommended to be the same as the class name.
String name;
An object stores its state in fields.
The state is a wider concept than OOP's property.
String name
field data type field name
A String class belongs to Java APIs.
A String class is used to express a series of characters.
int totalAbsenceDays;
int totalAbsenceDays
field data type field name
A int is a data type for integer.
void absent() {..}
if absent() does not return a value, you have to type void before absent().
if absent() return a value, you have to type the data type befere absent().
In Java, absent() is called a method not a function.

Let's make Student executable alone.
Open Student.java and add main method, as below:

class Student {
	String name;
	int totalAbsenceDays;
	void absent() {
		totalAbsenceDays = totalAbsenceDays + 1;
	public static void main(String[] args) {
		Student tom = null;
		Student will = null;

		tom = new Student();
		tom.name = "Thomas Edison";

		will = new Student();
		will.name = "William Blake";


C:\ Command Prompt
C:\Users\kim>javac Student.java

C:\Users\kim>java Studnet


Typing "java Student" drive a new JVM start and the main method of Student class start.
Whenever a JVM is started, the classloader is responsible for loading Java classes that make up the program, such as:
Student, String, System, etc.
A classloader already know the location of classes corresponding to Java APIs, e.g., String, System, etc.
Maybe you have to notify the classloader the location of the Student class.
If you want to run in a directory where is no Student class, you have to notify the location of Student class to the classloader using java -cp option.

public static void main (String[] args) {..}
The main method is the starting point of a Java pure application.
You have to make the main method in the starting class.
Student tom = null;
This statement declare the reference variable to refer to the Student object and initializes variable to null.
A reference variable saves a reference value which is used to access a object.
A reference variable dose not save a object itself.
If you make reference variable does not point any object, you assigned variable to null.
Since null is also value, Student tom; and Stundent tom = null; is a completely different.
Student tom; Student tom = null;
value of the tom has not been determined.
(It will be expressed as "not initialized.")
null is used to initialize the reference variable.
null is a value that indicates that don't refer to any object.
tom = new Student();
This statement assigns the reference value of a Student object generated to the Student type reference variable (tom).
if new Student(); runs, the Student object is created in the heap memory space and the reference value required to operate the generated object is returned.
tom.name = "Thomas Edison";
This statement set the name of student objects by using the reference variable (tom).
Usage: Reference variable and the following dott and following the fields or methods of the object.
This statement call the absent() method of the student which variable tom refer to.


In the basic example of Java, in order to display the results, so that the frequent use of this method.
Below shows all of the uses for this method.

class StandardOutput {
    public static void main(String[] args) {
        System.out.println(true);// boolean ouput and line break
        System.out.println('A');// 'A' output and line break
        char[] x = {'A','B','C'};
        System.out.println(x);//char[] value output and line break
        System.out.println(99.9);//double value output line break
        System.out.println();// simply line break
        System.out.println(99.9F);//float value output and line break
        System.out.println(100);//int value output and line break
        System.out.println(40000000L);//long value output and line break
        System.out.println(System.out);//Object's data type@hashcode ouput and line break
        System.out.println("standard output method");//String value output and line break
C:\ Command Prompt
C:\>javac StandardOutput.java

C:\>java StandardOutput

standard output method

The only difference between System.out.print() and System.out.println() is that System.out.print() does not change the line after the output.