Skip to content
Related Articles

Related Articles

Java Program to Create an Object for Class and Assign Value in the Object Using Constructor
  • Difficulty Level : Basic
  • Last Updated : 11 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

Ravi

Example 2:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        
    }
}

chevron_right


Output:

The company name is Apple
The configuration  is i5
Its Cost is 50000
Its operating System  IOS

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :