Implement a parameterized constructor to initialize an object
The Person class in Java represents a simple entity with a name attribute. It contains a constructor that initializes the name attribute with the provided value. In the main method, we demonstrate creating an instance of the Person class with the name "Sam Kumar" and then print the name of the person. This class provides a basic example of creating and using a class with a constructor in Java.
class Person { String name; Person(String n) { name = n; } public static void main(String[] args) { Person person = new Person("Sam Kumar"); System.out.println("Name : " + person.name); } }
Name : Sam Kumar
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions