Explanation
This Java code demonstrates the fundamental concepts of Object-Oriented Programming (OOP) by defining a Student class and then creating multiple objects (instances) of that class.
-
class StudentDefinition: The first part of the code defines a blueprint namedStudent. This blueprint describes what characteristics a student will have (theirname,rollno, andmarks) and what actions they can perform (adisplay()method to show their details).String name, rollno;: These are "data members" or "attributes" that will hold text values for a student's name and roll number.double marks;: This data member will hold a decimal number for a student's marks.void display(): This is a "method" or "behavior." When called, it prints the specificname,rollno, andmarksof the student object it's called on to the console.
-
class progx1andmainmethod: The second class,progx1, contains themainmethod. In Java, themainmethod is the entry point where the program execution begins.Student s1 = new Student();: Insidemain, the first line creates a newStudentobject from theStudentblueprint. This object is given a name,s1, which acts as a reference to it in memory.Student s2 = new Student();: Similarly, a second distinctStudentobject is created and referenced bys2. At this point,s1ands2are separate entities, each with their ownname,rollno, andmarksattributes, though initially these attributes have default values (null for String, 0.0 for double).- Assigning Values: The next lines assign specific values to the attributes of each student object:
s1.name = "Nathan";assigns "Nathan" to thenameattribute of thes1object.s1.rollno = "25LEC07";assigns "25LEC07" tos1's roll number.s1.marks = 9;assigns 9.0 tos1's marks.- The same process is repeated for
s2, assigning "Ashwin", "25LEC01", and 10 to its respective attributes.
- Calling Methods:
s1.display();: This line calls thedisplay()method on thes1object. This will execute the code withins1'sdisplay()method, printing "Nathan", "25LEC07", and 9.0.s2.display();: This line then calls thedisplay()method on thes2object, which will print "Ashwin", "25LEC01", and 10.0.
In essence, the code defines what a "student" is (a class), then creates two specific "students" (objects) with their own unique data, and finally asks each student object to display its own information.