2.4. How classes are in Java Programming world?

While developing classes as a Java developer on your job, you will never have main method in a class, usually you will instantiate the class and call the methods on it from another class.Let's take a relook at the original Car and Stdent classes we created earlier without the main method.

			package javatutorial;

			/**
			 * Example of a class
			 * 
			 * @author f5java.com
			 */
			public class Car {
				// Fields : value in fields represent the state of the class
				private boolean ignition;
				private String gear;
				private String fuelTankStatus;
				private boolean musicPlayer;				
				
				// Methods: represent the behavior of the class
				
				/**
				 * Displays car is moving forward message
				 */
				public void moveforward(){
					System.out.println("Moving the car in forward direction");
				}
				
				/**
				 * Displays car is moving backwards message
				 */
				public void moveBackwards(){
					System.out.println("Moving the car in reverse direction");
				}
				
				/**
				 * Displays car is turning right message
				 */
				public void turnRight(){
					System.out.println("Turning car right");
				}
				
				/**
				 * Displays car is turning left message
				 */
				public void turnLeft(){
					System.out.println("Turning car left");
				}

				/**
				 * Displays car status
				 */
				public void displayCarStatus(){
					System.out.println("Ignition: " + ignition);
					System.out.println("Gear: " + gear);
					System.out.println("Fuel Tank: " + fuelTankStatus);
					System.out.println("musicPlayer " + musicPlayer);
				}
			}
		
			package javatutorial;

			/**
			 * Example class, showing declaration of fields and methods in a class
			 * 
			 * @author f5java.com
			 */
			public class Student {
				private String name;
				private String course;
				private String grade;
				
				Student (String name, String course, String grade){
					this.name = name;
					this.course = course;
					this.grade = grade;		
				}
				
				/**
				 * Displays student details
				 */
				public void displayStudentDetails(){
					System.out.println("Name: " + name);
					System.out.println("Course: " + course);
					System.out.println("Greade: " + grade);
				}

			}
		

To instantiate and call methods on these classes, we will create a Tester class. However to keep this tutorial simple, we still have a Tester Class which will have main method.

			package javatutorial;

			/**
			 * Tester class: used to create instances and call methods of an onject
			 * 
			 * @author f5java.com
			 */
			public class Tester {

				public static void main(String[] args) {
					
					// 1 . Instantiate car object
					Car carObj = new Car();
					
					// 2. call methods on the car object
					carObj.displayCarStatus();
					
					// 3. Instantiate Student object
					Student student = new Student("Henry", "Java programming course @ f5java.com", "A");
					
					// 4. call method on the student object
					student.displayStudentDetails();
				}
			}