Classes and Objects
One of the major advantages of using object-oriented programming is that programming can create modules that are reusable modules that need to be changed when a new type of object is added.
While having basic knowledge of the java programming language you can learn to write your own classes. In this article, you can learn about creating or defining your own classes, declaring member variables, methods, and constructors.
Class
A class is a combination of variables and methods. In real world-class refers to a combined unit having different variables and methods in it.
For Example: In daily-life “House” is a class, having Television, Refrigerator, etc as variables and Kitchen. Rooms etc as methods.
Declaring a class
We can create a class followed by a class keyword followed by class_name. We have seen a class defined in the following way,
This is the class declaration, the class body(between the curly braces}, declarations for the fields that provide the state of class and its objects.
Naming Convention for class
ClassName should be in camel-case i.e each and every word's first letter must be uppercase.
Class declarations can include the following considerations which are,
-
Access Modifiers like public, private, protected, default(no modifier).
-
ClassName must be in camel case order.
-
The class body is surrounded by {}.
Understanding Class Members
Class Variables
In the case of the Employee class, the instance variables are id, name, salary, email. Each employee object has its own values for these variables, stored in different memory locations.
- class Employee {
- public int emp_id;
- public String emp_name;
- public double emp_salary;
-
- private int id;
-
-
- private static int numberOfEmployees = 0;
- }
Class variables are referenced by the class name itself, as in
- Employee.numberOfEmployees
This makes it clear that they are class variables.
Class Methods
The Java language provides static methods and variables. Static methods, having static modifiers in their declaration should be invoked by class name, without the need for creating an instance of the class.
Syntax - Classname.methodname(params)
For Example - Adding a static method to the Employee class to access numberOfEmployees static field.
- public static int getNumberOfEmployees() {
- return numberOfEmployees;
- }
Declaring Member Variables
A Class contains method definitions and variable declaration. There are several types of variables.
-
Variables inside the class - instance variables.
-
Variables inside a block of code or method - local variables.
-
Variables in a class - member variables.
Variables may be instance variables, static variables, or final variables. For example, The Employee class uses the following lines of code.
- public int emp_id;
- public String emp_name;
- public double emp_salary;
The fields of Employee are named emp_id, emp_name, emp_salary. The public access modifier is accessible by any object of the class. Here below is the full description of an Employee class.
- class Employee {
-
- public int emp_id;
- public double emp_salary;
- public String emp_name
- public String emp_email;
-
- public int getEmp_id() {
- return emp_id;
- }
- public void setEmp_id(int emp_id) {
- this.emp_id = emp_id;
- }
- public int getSalary() {
- return emp_salary;
- }
- public void setSalary(int emp_salary) {
- this.emp_salary = emp_salary;
- }
- public String getName() {
- return emp_name;
- }
- public void setName(String emp_name) {
- this.emp_name = emp_name;
- }
- public String getEmail() {
- return emp_email;
- }
- public void setEmail(String emp_email) {
- this.emp_email = emp_email;
- }
-
- @Override
- public String toString() {
- return "Employee [emp_id = " + emp_id + ", emp_salary = " + emp_salary + ", emp_name = " + name + ", emp_email = " + emp_email + "]";
- }
- }
The fields of Employee are id, name, salary, email is instance variables and setter getter methods are instance methods.
Objects & References
Once a class is defined, you can declare a variable of type class. A Java program creates many objects, thus a program carries out various tasks. Once an object has completed the work, its resources are recycled for use by other objects.
Creating Objects
A class provides the blueprint for objects, we can create an object from the class. Here the object of the employee class is created
- Employee emp;
- Emp = new Employee();
-
- Employee emp = new Employee();
Instantiating a Class
A class is instantiated using a newkeyword as it invokes the object constructor and allocates memory for a newobject and returns the reference of that memory. The newoperator returns a reference to the object is created. This reference is usually assigned to a variable of the appropriate type.
The newoperator is used to create an object of that reference type, where emp is the object reference and Employee() is an object.
Using Objects
Once an object is created we will use that object for some manipulation or retrieve data with that object. We may use this object with one of its fields, or call one of its methods to perform any action.
Referencing an object’s field
Once an object is created then object fields are accessed by their name. Eg: we can add a statement within the Employee class that prints the Id and name of the employee.
- System.out.println("Id and name is: " + id+ ", " + name);
Code that is outside the class must use an object reference followed by dot(.) operator, followed by simple name,
- objectRefrence.fieldName;
Calling an object’s Method
We use an object reference to invoke an object’s method. We append the method’s name with object reference with a dot(.) operator.
Syntax - objectReference.MethodName(args); or objectReference.MethodName();
The Employee class has some getter methods like getName(), getEmail(), getId(), and more, that represents the name, Email Id, Id of the employee respectively. Here’s employee object invokes the object as,
- System.out.println("Name of Employee: " + emp.getName());
- System.out.println("Email Id of Employee: " + emp.getEmail());
- System.out.println("Id of Employee: " + emp.getID());
These above methods execute and display the results.Remember, invoking a method on a particular object is the same as sending a message to that object.
Summary
Thus, coming towards the end of this article, we had learned about Classes and Objects and how to use them with Java.
What did we learn?
-
What is a Class?
-
Declaring a Class.
-
Understanding Class Members.
-
Declaring Member Variables.
-
Objects: Creation & Using objects
-
Instantiating a class.
-
Referencing an object’s field.
-
Calling an object’s method.