Java Program to Create an Object for Class and Assign Value in the Object Using Constructor Last Updated : 11 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Java is one of the most popular programming languages. It is an object-oriented programming language which means that we can create classes, objects, and many more. It also supports inheritance, polymorphism, encapsulation, and many more. It is used in all applications starting from mobile applications to web-based applications. Classes are defined as the blue-print or template from which we can create objects and objects are the instances of the class which consist of states and behaviors. Approach: First, define a class with any name 'SampleClass' and define a constructor method.The constructor will always have the same name as the class name and it does not have a return type.Constructors are used to instantiating variables of the class.Now, using the constructors we can assign values.After the constructor method, implement a function that returns the value of any variables.Now in the main function create an object using the 'new' keyword. If there is no user-defined constructor of the class, then the default constructor of the class is called else when the object is created the user-defined constructor is called as per the type and number of parameters matched with the constructor of the class. We can call the above-declared function using the object.At last print the value from the function using the System.out.println() statement.Once you are done with the implementation run the program and will get the output.Example 1: Java // Java program to show the class declaration // and how to create an instance of this class // Class Declaration public class Student { // Instance Variables String name; String course; int age; // Constructor Declaration of Class public Student(String name, String course,int age) { this.name = name; this.course = course; this.age = age; } // method 1 public String getName() { return name; } public static void main(String[] args) { // creating object using new operator Student s1 = new Student("Ravi","CSE",23); System.out.println(s1.getName()); } } Output: Ravi Example 2: Java // Java program to show the class declaration // and how to create an instance of this class // Class Declaration public class Computer { // Instance Variables String name; String config; int cost; String os; // Constructor Declaration of Class public Computer(String name, String config, int cost, String os) { this.name = name; this.config = config; this.cost = cost; this.os = os; } // method 1 public String getName() { return name; } // method 2 public String getConfig() { return config; } // method 3 public int getCost() { return cost; } // method 4 public String getOs() { return os; } public static void main(String[] args) { // creating object using new operator Computer c1 = new Computer("Apple","i5", 50000, "IOS"); System.out.println("The company name is "+ c1.getName()); System.out.println("The configuration is "+ c1.getConfig()); System.out.println("Its Cost is "+ c1.getCost()); System.out.println("Its operating System "+ c1.getOs()); } } Output: The company name is Apple The configuration is i5 Its Cost is 50000 Its operating System IOS Comment More infoAdvertise with us Next Article How to Create a Date Object using the Calendar Class in Java B bunnyram19 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads How to Create a Date Object using the Calendar Class in Java In Java, the Calendar class can provide a flexible way to handle the dates and times. This article demonstrates how to create the Date object using the Calendar class by setting the specific date and time and converting it to the Date object and printing the exact date and time. This approach offers 2 min read Java Program to Illustrate the Availability of Default Constructor of the Super Class to the Sub Class by Default A constructor in Java is a special method that is used to initialize an object. Whenever an object is created using the new() keyword at least one construction is called. The constructor name must match with the class name and cannot have a return type. If there is no constructor available in the cl 5 min read How to Initialize HashSet Values Using Constructor in Java? In Java programming, HashSet is a collection framework that is implemented by the HashSet class. It only contains unique elements and does not allow duplicate values. Mainly it does not maintain any insertion order but is inserted based on hash code. We can initialize HashSet values in many ways in 2 min read Four Main Object Oriented Programming Concepts of Java Object-oriented programming generally referred to as OOPS is the backbone of java as java is not a purely object oriented language but it is object oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are l 7 min read Java Program to Convert Byte Array to Object Converting byte array into Object and Object into a byte array process is known as deserializing and serializing. The class object which gets serialized/deserialized must implement the interface Serializable. Serializable is a marker interface that comes under package 'java.io.Serializable'.Byte Arr 2 min read Java Program to Convert String to Object In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object. Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the me 2 min read Like