Master Java with Us!
Enjoying this guide? Subscribe to Ram N Java for straightforward, beginner-friendly programming tutorials!
Subscribe on YouTubeWhat is an Object in Java? Student Class and Objects Explained
In Java Object-Oriented Programming (OOP), a class acts as the blueprint or template, while an object is a concrete instance created from that blueprint. To truly understand how objects work in memory and in code, let us look at a real-world example: modeling a Student.
1. Defining States and Behaviors in Student Class
Every student entity possesses characteristics (states) and actions they can perform (behaviors):
- States (Attributes / Variables):
name,age,color, andsex. - Behaviors (Actions / Methods):
eating(),drinking(), andrunning().
2. Creating Multiple Objects from One Blueprint
Using a single Student blueprint, we can create multiple distinct student instances with unique state values:
- Student 1 (Peter): Age 3, white, male, eats more food, drinks more water, runs fast.
- Student 2 (John): Age 4, brown, male, eats less food, drinks more water, runs slow.
- Student 3 (Julia): Age 2, white, female, eats less food, drinks less water, runs fast.
3. Syntax for Object Creation in Java
In Java, an object is instantiated using the new keyword followed by the constructor:
Here, the left side (ClassName objectName) represents the reference type, and the right side (new ClassName()) represents the actual object type allocated in memory.
4. Code Example: Student and StudentDemo
Student.java
public class Student {
// States / Attributes
String name;
int age;
String color;
String sex;
// Behaviors / Methods
public void eating() {
System.out.println(name + " is eating.");
}
public void drinking() {
System.out.println(name + " is drinking water.");
}
public void running() {
System.out.println(name + " is running.");
}
}
StudentDemo.java
public class StudentDemo {
public static void main(String[] args) {
// Creating student object for Peter
Student peter = new Student();
peter.name = "Peter";
peter.age = 3;
peter.color = "White";
peter.sex = "Male";
displayStudentInfo(peter);
peter.eating();
peter.drinking();
peter.running();
}
public static void displayStudentInfo(Student student) {
System.out.println("Name: " + student.name);
System.out.println("Age: " + student.age);
System.out.println("Color: " + student.color);
System.out.println("Sex: " + student.sex);
}
}
5. Key Takeaways
- A class defines what properties and methods every object of that type will have.
- Each object maintains its own separate copy of instance variables.
- Methods execute actions based on the object's specific internal state.
Watch More Java Tutorials from Ram N Java
- Java Object Class Explained | Java Tutorial
- Java Object Class: How to Use the getClass Method | Java Tutorial
- Java Object Class: Subclass Assignment Explained | Java Tutorial
Click here to watch on YouTube:
https://www.youtube.com/watch?v=pl-G-4q71No&list=UUhwKlOVR041tngjerWxVccw
Click the below Images to Enlarge:
Student.java
/** * Student class is the blueprint from which individual student objects can be * created. */ public class Student { String name; int age; String color; String sex; public void eating(String string) { System.out.println(name + " " + string); } public void drinking(String string) { System.out.println(name + " " + string); } public void running(String string) { System.out.println(name + " " + string); } }
StudentDemo.java
public class StudentDemo { public static void main(String[] args) { Student peter = new Student(); /* * Setting States/Attributes/Properties/Characteristics. */ peter.name = "Peter"; peter.age = 3; peter.color = "white"; peter.sex = "male"; displayStudentInformation(peter); System.out.println("\nBehaviors"); System.out.println("----------"); peter.eating("can eat more foods"); peter.drinking("can drink more water"); peter.running("can run fast"); System.out.println("*********************************************"); Student john = new Student(); /* * Setting States/Attributes/Properties/Characteristics. */ john.name = "John"; john.age = 4; john.color = "brown"; john.sex = "male"; displayStudentInformation(john); System.out.println("\nBehaviors"); System.out.println("----------"); john.eating("can eat less foods"); john.drinking("can drink more water"); john.running("can run slow"); System.out.println("*********************************************"); Student julia = new Student(); /* * Setting States/Attributes/Properties/Characteristics. */ julia.name = "Julia"; julia.age = 2; julia.color = "white"; julia.sex = "female"; displayStudentInformation(julia); System.out.println("\nBehaviors"); System.out.println("----------"); julia.eating("can eat less foods"); julia.drinking("can drink less water"); julia.running("can run fast"); System.out.println("*********************************************"); } private static void displayStudentInformation(Student student) { System.out.println("Attributes"); System.out.println("--------------"); System.out.println("Name : " + student.name); System.out.println("Age : " + student.age); System.out.println("Color : " + student.color); System.out.println("sex : " + student.sex); } }
Output
Attributes -------------- Name : Peter Age : 3 Color : white sex : male Behaviors ---------- Peter can eat more foods Peter can drink more water Peter can run fast ********************************************* Attributes -------------- Name : John Age : 4 Color : brown sex : male Behaviors ---------- John can eat less foods John can drink more water John can run slow ********************************************* Attributes -------------- Name : Julia Age : 2 Color : white sex : female Behaviors ---------- Julia can eat less foods Julia can drink less water Julia can run fast *********************************************
https://sites.google.com/site/javaee4321/java/StudentDemoObjectApp.zip?attredirects=0&d=1
No comments:
Post a Comment