Krivalar Tutorials 
Krivalar Tutorials

Java Classes and Methods


<Hello World

Interfaces >





class

All java keywords are case sensitive and should be in lowercase only. The keyword class should be in lowercase only

A class is a type definition of a real world or an application entity or object.

For example, if you consider Car as a class. Each specific car becomes an object. In other words, to represent different specific cars, you need to create a class named Car.

A class always has properties and behaviours.

class Car{
	//members
}

For example, Car class can have the following properties and represented in Java as member variables of class.

Car class can have behaviors such as:

These properties and behaviours would be written as member variables and methods in a class as shown below

public class Car{

	String engineName;
	int numberOfTyres;
	String tyreType;
	String bodyMetal;
	int wheelWeight;

	void driveCar(){
	}

	int applyBrakes() {
		//onsuccess
		return 1;
	}

	void turnSteeringWheel() {
	}

	String startEngine() {
		//onsuccess
		return "STARTED";
	}

	void onAC() {
	}

	void onWipers() {
	}
}

Similarly, you can create class to represent anything:

and so on.

Let us look at a different example for creating a class: Employee

class - Example

public class Employee{
	int id;
	String name;
	float salary;

	public Employee(int id,
			String name,
			float salary)
	{
		this.id= id;
		this.name= name;
		this.salary= salary;
	}
	public void printEmployeeDetails()
	{
		System.out.println(
			"Salary of Employee "
			+ name
			+ " with ID " + id
			+ " " + salary);
	}

	public static void main(
					String args[])
	{
		Employee emp = new Employee(
						2, "Sachin",
						25000.00f);
		emp.printEmployeeDetails();
	}
}

You can create classes for anything generic in nature. Once you create one class, you can create one or several objects with it.

ClassObjectDescription
StudentEach studentFor example, if you create a class named Student, you can create several Student objects
Car Each carFor example, if you create a class named Car, you can create several car objects or instances with different vehicle numbers
EmployeeEach employeeFor example, if you create a class named Employee, you can create employee instances or objects


Usual High level Ways to create Objects

When you create a software application, you create one or more classes you need.

For each class, You can

You can create objects in the way, you need your application to work.

Ways To Create An Application

Most applications include the following actions and more..


Java Class Components

Everything in Java except for the package, import statements, enums and interfaces, everything else is defined within a Class.

In Java, A class can have:


Java Objects

An object is an instance of a Java class. There are several ways to create a Java object.

We will look at simple ways to create an object.

public class MyApplication{
public static void main(String a[]) {
	Employee emp = new Employee(
					2, "Harry",
					25000.00f);
	emp.printEmployeeDetails();
	Employee emp2;
	try {
		emp2 = emp.clone();
	} catch(
		CloneNotSupportedException e){
		e.printStackTrace();
	}
}

}

More ways to create objects

If you are interested to know more, you can go through the below list. However, you might want to go through this list when you have gained some level of expertise in Java.


Java Methods

Any operation or behaviour is defined by a method in Java. Methods are similar to Functions in other programming languages. A method is defined within a class.

A method can take zero, one or more parameters as input and return one parameter as output

Examples of methods:

Method below takes no parameter as input and returns void. It is a public method and can be invoked on an instance within the same or different package.

public void invoke(){
		System.out.println(
			"I am invoked");
	}

Method below takes no parameter as input and returns long value. It is a private method and can be invoked only within class and not on an instance.


private long getID(){
	long id =
		System.currentTimeMillis();
	return id;
}

Method below takes two int parameters as input and returns one int value. It is a default method (meaning, with no private or public qualifier mentioned) and can be invoked on an instance which is within the same but not different package.


 int findSum(int x, int y){
	int z = x + y;
	return z;
}


<Hello World

Interfaces >