Java Classes and Methods

<<Previous

Next >>




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

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

Ways To Create An Application

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

  • You can create objects when the application starts up.
  • You create one or more objects for the class during progam execution.
  • You can let user actions or system events create the objects from a class dynamically.
You can create objects in the way, you need your application to work.


Java Class Components

Everything in Java except for the import statements are defined within a Class.

In Java, A class can have:

  • Fields - fields are represented as variables in Java. Fields can store values. In regular usage, Fields are also sometimes referred to as properties or attributes
  • Methods - methods are also referred to as behaviours. Methods in Java are similar to functions in C and C++. Methods have lines of code which perform some actions.
  • Static Blocks - static blocks contain actions which need to be performed as soon as a class is loaded
  • Nested Classes - classes defined within classes are called nested classes. We would learn nested classes later in the tutorial

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.

  • creating the 'new' operator or
  • using the Object.clone() method

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

  • using reflection using the Constructor.newInstance()
  • using reflection using the Class.newInstance()
  • objects created for anonymous classes
  • objects created for Lambda expressions
  • objects created when deserializing
  • objects created because of autoboxing
  • arrays are created as objects by default
  • varargs using Object... in method calls are created as arrays and hence as objects by default
  • Strings created with " " (double quotes) are actually objects

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 Example of a method:
	public void invoke(){
		System.out.println("I am invoked");
	}

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

<<Previous

Next >>