DEV Community

Shrinidhi S
Shrinidhi S

Posted on

Object creation

public class Calculator
{
public static void main(String[] args)
{
Calculator casio = new Calculator(); //Constructor
}
public void add()
{
System.out.println(10+20);
}

}
Enter fullscreen mode Exit fullscreen mode

In java, we have a keyword called "new" which should be followed by class name.
Class name should start with capital letter.

new Calculator;
Enter fullscreen mode Exit fullscreen mode

Here, an object is created and then we have to give name.

calculator casio = new Calculator; //constructor
Enter fullscreen mode Exit fullscreen mode

Here casio is object's name.
calculator casio - object's representation.
new Calculator - object creation.

Structure of object creation:

Classname reference_name = new Classname;
Enter fullscreen mode Exit fullscreen mode

What is a method?
A method is a block of code that performs a specific task and can be called for execution by its name.

public void add()
Enter fullscreen mode Exit fullscreen mode

If it contains () it is definitely a method.
main(String[] args) - main method
add() - add method

Top comments (0)