Java Program to Create an Object for Class and Assign Value in the Object Using Constructor
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.
- 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.
The company name is Apple The configuration is i5 Its Cost is 50000 Its operating System IOS
My Personal Notes arrow_drop_up
Please Login to comment...