Java Program to Create an Object for Class and Assign Value in the Object Using Constructor Last Updated : 11 Nov, 2020 Comments Improve Suggest changes 2 Likes 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 Create Quiz Comment B bunnyram19 Follow 2 Improve B bunnyram19 Follow 2 Improve Article Tags : Java Java Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like