java program to print student info by using class object method with code examples

Java Program to Print Student Info using Class Object Method

Introduction

In this article, we will learn how to create a Java program to print student information using class objects and methods. This will help you understand how objects and methods work in Java, and how you can use them to create more complex programs.

What are Classes and Objects in Java?

A class in Java is a blueprint for creating objects, which are instances of a class. A class can have fields and methods, and it defines the behavior and state of its objects. An object is an instance of a class and can be used to access its fields and methods.

Creating a Class for Student Info

Let's create a class for student information. This class will have fields for the student's name, roll number, and marks. Here's the code for the Student class:

class Student {
   String name;
   int rollNo;
   int marks;
   
   // Method to display the student's information
   void display() {
      System.out.println("Name: " + name);
      System.out.println("Roll No: " + rollNo);
      System.out.println("Marks: " + marks);
   }
}

The above code defines a class named Student, with fields for name, rollNo, and marks. It also has a method named display that prints the student's information.

Creating Objects and Using the Method

Next, we'll create objects for the Student class and use the display method to print the student's information. Here's the code for creating objects and using the display method:

class Test {
   public static void main(String args[]) {
      // Creating objects
      Student s1 = new Student();
      Student s2 = new Student();
      
      // Initializing the fields of the objects
      s1.name = "John";
      s1.rollNo = 1;
      s1.marks = 90;
      
      s2.name = "Jane";
      s2.rollNo = 2;
      s2.marks = 95;
      
      // Using the display method to print the student's information
      System.out.println("Student 1 Information");
      s1.display();
      System.out.println("\nStudent 2 Information");
      s2.display();
   }
}

In the above code, we created two objects of the Student class named s1 and s2. We then initialized the fields of the objects with the student's information. Finally, we used the display method to print the student's information.

Output

Student 1 Information
Name: John
Roll No: 1
Marks: 90

Student 2 Information
Name: Jane
Roll No: 2
Marks: 95

Conclusion

In this article, we learned how to create a Java program to print student information using class objects and methods. We created a class for student information and used objects to access its fields and methods. This simple example demonstrates the basic concepts of classes and objects in Java, and how you can use them to create more complex programs.
Class Constructors

In Java, a constructor is a special method that is used to initialize an object when it is created. A constructor has the same name as the class and doesn't have a return type. If a class doesn't have a constructor, Java automatically provides a default constructor. However, if you want to initialize an object with specific values, you can define a constructor in the class.

Here's an example of a class with a constructor:

class Student {
   String name;
   int rollNo;
   int marks;
   
   // Constructor
   Student(String name, int rollNo, int marks) {
      this.name = name;
      this.rollNo = rollNo;
      this.marks = marks;
   }
   
   // Method to display the student's information
   void display() {
      System.out.println("Name: " + name);
      System.out.println("Roll No: " + rollNo);
      System.out.println("Marks: " + marks);
   }
}

In the above code, we defined a constructor for the Student class that takes three parameters: name, rollNo, and marks. The constructor initializes the fields of the class with the values passed as parameters.

When creating an object, you can pass the values for the fields as arguments to the constructor. Here's an example:

class Test {
   public static void main(String args[]) {
      // Creating objects and passing arguments to the constructor
      Student s1 = new Student("John", 1, 90);
      Student s2 = new Student("Jane", 2, 95);
      
      // Using the display method to print the student's information
      System.out.println("Student 1 Information");
      s1.display();
      System.out.println("\nStudent 2 Information");
      s2.display();
   }
}

In the above code, we created two objects of the Student class and passed the values for the fields as arguments to the constructor. This way, the fields are initialized with the values passed to the constructor, and you don't have to initialize the fields separately.

Access Modifiers

In Java, access modifiers are used to control the visibility of a class, its fields, and methods. There are four access modifiers in Java:

  1. public: A public member is accessible from anywhere in the program.
  2. private: A private member is only accessible within the same class.
  3. protected: A protected member is accessible within the same class and its subclasses.
  4. default (no keyword): A default member is accessible within the same package.

Here's an example of a class with access modifiers:

class Student {
   private String name;
   private int rollNo;
   private int marks;
   
   // Constructor
   public Student(String name, int rollNo, int marks) {
      this.name = name;
      this.rollNo = rollNo;
      this.marks = marks;
   }
   
   // Method to display the student's information
   public void display() {
      System.out.println("Name: " + name);
      System.out.println("Roll No: " + rollNo);
      System.out.println("Marks: " + marks);
   }
}

In the above code, we used

Popular questions

  1. What is a class in Java?

A class in Java is a blueprint for creating objects that defines variables and methods common to all objects of a certain kind.

  1. What is an object in Java?

An object in Java is an instance of a class. An object has its own state and behavior, defined by the fields and methods of the class.

  1. What is a method in Java?

A method in Java is a block of code that performs a specific task. A method can take parameters, return a value, or return nothing. Methods are defined inside a class and can be called by creating an object of the class.

  1. What is the purpose of a constructor in Java?

A constructor in Java is a special method that is used to initialize an object when it is created. A constructor has the same name as the class and doesn't have a return type. The purpose of a constructor is to set the initial values for the fields of an object.

  1. What is the difference between a class and an object in Java?

A class in Java is a blueprint or a template for creating objects, while an object is an instance of a class. A class defines the variables and methods that are common to all objects of a certain kind, while an object has its own state and behavior defined by the fields and methods of the class.

Tag

OOP (Object-Oriented Programming)

Posts created 2498

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top