Object and Class

Object

Objects are crucial to understanding OOP.
There are many examples of real-world objects, such as your cup, computer, etc.
Real-world objects have three 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 is only one.
You are the only one.

Software objects

Software objects are similar to natural world objects.
Software objects are a combination of data and functions.
You can consider the property of the real-world object as data and the behavior of the real-world object as a function.

Class

A class is a casting mold from which JVM creates individual objects.
To create an object in a Java program, you must first create a class.

Let's think that we have to develop a program for student attendance management. We can 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.

Student.java
class Student {
  String name;
  int totalAbsenceDays;
	
  void absent() {
    totalAbsenceDays = totalAbsenceDays + 1;
  }
}
class Student {..}
The class name comes after the class keyword.
Make the file name the same as the class name.
String name;
An object stores its state in fields.
The state is a broader concept than OOP's property.
String name
field data type field name
String class belongs to Java APIs.
You can express a series of characters using the String class.
int totalAbsenceDays;
int totalAbsenceDays
field data type field name
An int is a data type for an integer.
void absent() {..}
if absent() does not return a value, you must type void before absent().
if absent() returns a value, you must type the data type before absent().
In Java, absent() is called a method, not a function.

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

Student.java
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";

    tom.absent();
    will.absent();
    tom.absent();

    System.out.println(tom.totalAbsenceDays);
    System.out.println(will.totalAbsenceDays);
  }
}
C:\ Command Prompt
C:\Users\John>javac Student.java

C:\Users\John>java Studnet
2
1

C:\>

Typing "java Student" drives a new JVM start, and the method named main of Student class start.
Whenever a JVM start, the classloader is responsible for loading Java classes that make up the program, such as Student, String, System, etc.
A classloader already knows the location of classes corresponding to Java APIs, e.g., String, System, etc.
Maybe you have to notify the classloader of 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 the Student class to the classloader using the cp option.

public static void main (String[] args) {..}
This method is the starting point of a Java pure application.
You have to make the method named main in the starting class.
Student tom = null;
This statement declares the reference variable tom to refer to the Student object and initializes the variable tom to null because it does not yet refer to anything.
A reference variable saves a reference value used to access an object. Note that there are no objects inside the reference variables box.
You assign null to a reference variable to make the variable not refer to anything.
Since null is also a value, the following two statements are entirely different.
Student tom; Student tom = null;
The variable tom in the first statement has no value. -- We express this as 'not initialized' -- You can assign null only to reference variables. -- null is a value that indicates that it doesn'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 named tom.
If 'new Student()' runs, JVM creates a Student object in the heap memory space, generates a reference value pointing to the student, and returns it.
tom.name = "Thomas Edison";
This statement sets the student's name using the reference variable (tom).
You can access an object by typing a reference variable followed by a dot followed by a field or method.
tom.absent();
This statement calls the absent() method of the student, to which variable tom refers.

System.out.println()

Below are all of the uses for this method.

StandardOutput.java
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:\Users\John>javac StandardOutput.java

C:\Users\John>java StandardOutput
true
A
ABC
99.9

99.9
100
40000000
java.io.PrintStream@de6ced
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.