In this article, you will be introduced with the basic OOPs concept i.e. Classes and Objects and how you can create classes and objects in your Java program.
Java Objects
Object refers to an entity that has state and behavior.
- State: represents the data (properties) of an object.
- Behavior: represents the behavior (functionality) of an object.
Let’s take few examples:
1 . car is an object
- It has model, color, weight, price as states.
- speedup, changing gears, driving, etc. behavior.
2 . account is an object
- It has account no, type, balance as states.
- deposit, withdrawal, etc, are its behavior.
Java Class
A Class is a way to bind the data describing an object and its associated methods together.
Classes are needed to represent real-world entities that not only have properties (their characteristics) but also have associated operations (their behavior).
How to define a class in Java?
Syntax
class ClassName { //variables //methods }
Example
class Account { private String type; private int accountno; private float balance; public void deposit(float amount) { balance = balance + amount; } public void withdraw(float amount) { balance = balance - amount; } }
Example explained
- The data describing an account (i.e., account no, type and balance) and its associated operations (deposit and withdraw) are bound together under one name Account (ClassName).
- These variables and methods defined within a class are called members of the class.
Notice two keywords private and public in above program. These are access modifiers which will be discussed in detail in later chapters. For now, just remember:
- The private keyword makes instance variables and methods private which can only be accessed inside the same class.
- The public keyword makes instance variables and methods public which can be accessed from outside the class.
Creating Objects
To access members defined within the class, you need to create objects.
- The objects of a class are created by using the “new” keyword.
- new allocates memory for the object.
- You can create multiple objects of the same class.
Syntax
Here, Constructor is a new term, we will discuss in detail in later chapters. For now, just remember:
- A Constructor is similar to a method(but not actually a method) with the same name as that of its class and it is used to initialize the objects with a legal initial value.
Example
class Account { private char type; private int accountno; private float balance; public void deposit(float amount) { balance = balance + amount; } public void withdraw(float amount) { balance = balance - amount; } } public class ClassMain { public static void main(String[] args) { //account1 and account2 are the objects of Account class Account account1 = new Account(); Account account2 = new Account(); } }
How to access members?
You can access members (call methods and access instance variables) by using the dot(.) operator.
For example, methods can be accessed as :
account.deposit();
This statement calls the deposit() method inside the Account class for an account object.
When you call the method using the above statement, all statements within the body of deposit() method are executed. Then, the control of the program jumps back to the statement following account.deposit().
Similarly, the instance variable can be accessed as:
account.balance;
Example: Java Class and Objects
class Student { //instance variables int rollno; String name; //methods public void insertRecord(int roll_no, String student_name) { rollno = roll_no; name = student_name; } public void displayDetail() { System.out.println(rollno + " " + name); } } public class ClassMain { public static void main(String args[]) { //s1 and s2 are two objects of Student class Student s1 = new Student(); Student s2 = new Student(); s1.insertRecord(101, "Manish"); s2.insertRecord(202, "Arun"); s1.displayDetail(); s2.displayDetail(); } }
Output
101 Manish 202 Arun
Example explained
In the above program,
- Student class consists of two instance variables rollno, name and two methods insertRecord() and displayDetail().
- Inside main() method, s1 and s2 are two objects of Student class.
- Here, we are initializing the value to these objects by invoking the insertRecord() method.
- And, we are displaying the state (data) of the objects by invoking the displayDetail() method.
We have mentioned the term method quite a few times. In the next chapter, you will learn about Java methods in detail.