INTRODUCTION TO CREATING CLASSES AND OBJECTS

Object-oriented programming languages group program variables with procedures to create program components called objects.

In Java, object-oriented programming resolves around a few key concepts:

OBJECTS

There are two steps to object creation

CREATING  A CLASS

The general form of creating a class

access     class   classname  [extends ...]   [implements ...]
{        [access] [static]  data type     instance-variable(s);

         [access] [static]   data type  method(s) (parameter list)
         {

                  // Method body

         }

}

To create a class, assign a name to it and decide what attributes (data) and methods (behaviors) it will have.
A class header has three parts

Public classes are accessible by all objects and may be extended, or used as the basis for another class. The public modifier is used for most classes

Instance variables, or fields, are placed between the curly brackets in the body of the class

Instance methods

Example
public class Student
{ private int stuNum;
  private String stuName;
 //Create two methods to get the data members
public int getStuNum( )
  { return stuNum;
  }
  public String getStuName( )
  { return stuName;
   }
  //Create two methods to set the values of two data members
public void setStuNum(int  n)
{  stuNum = n;
  }
  public void setStuName(String  Name)
  { stuName = Name;
   }
}

ORGANIZING CLASSES

CREATING OBJECTS

Example Assume  the class Student has been created and compiledt. Now create a class called TestStudent which will use the class Student. 
public class TestStudent
{ public static void main(String[ ] args)
   { Student stu1= new Student( );
      stu1.setStuNum(123);
      stu1.setStuName("John Nguyen");
      System.out.println("Student Number: " + stu1.getStuNum( ) );
      System.out.println("Student Name: " + stu1.getStuName( ) );
    }
}

Or two classes, Student and TestStudent, can be put in the same file called TestStudent. Only one class can be declared public in one file, and that's TestStudent in this case.
class Student
{ private int stuNum;
  private String stuName;
 //Create two methods to get the data members
public int getStuNum( )
  { return stuNum;
  }
  public String getStuName( )
  { return stuName;
   }
  //Create two methods to set the values of two data members
public void setStuNum(int  n)
{  stuNum = n;
  }
  public void setStuName(String  Name)
  { stuName = Name;
   }
}
public class TestStudent
{ public static void main(String[ ] args)
   { Student stu1= new Student( );
      stu1.setStuNum(123);
      stu1.setStuName("John Nguyen");
      System.out.println("Student Number: " + stu1.getStuNum( ) );
      System.out.println("Student Name: " + stu1.getStuName( ) );
    }
}

USING CONSTRUCTOR

Example

public class Student
{ private int stuNum;
  private String stuName;
  //Create a constructor to initialize data members
   Student( )
  { stuNum = 0;
     stuName = "zzzz";
  }
  //Create a constructor to receive data
   Student(int n, String Name)
   { stuNum = n;
      stuName = Name;
   }
 //Create two methods to get the data members
public int getStuNum( )
  { return stuNum;
  }
  public String getStuName( )
  { return stuName;
   }
  //Create two methods to set the values of two data members
public void setStuNum(int  n)
{  stuNum = n;
  }
  public void setStuName(String  Name)
  { stuName = Name;
   }
}

public class TestStudent
{ public static void main(String[ ] args)
   { Student stu1= new Student( );
      Student stu2 = new Student(100, "Linda");
      Student stu3 = new Student( );
      stu1.setStuNum(123);
      stu1.setStuName("John Nguyen");
      System.out.println("Student Number: " + stu1.getStuNum( ) );
      System.out.println("Student Name: " + stu1.getStuName( ) );
      System.out.println("Student Number: " + stu2getStuNum( ) );
      System.out.println("Student Name: " + stu2.getStuName( ) );
      System.out.println("Student Number: " + stu3.getStuNum( ) );
      System.out.println("Student Name: " + stu3.getStuName( ) );
    }
}

Output
Student Number: 123
Student Name: John Nguyen
Student Number: 100
Student Name: Linda
Student Number: 0
Student Name: zzzz