Java: Object creation

This entry is part 21 of 54 in the series Learn Java

To access a class’s fields and methods from outside the defining class, an object of the class must first be created.

This is done by using the new keyword, which will create a new object in the system’s memory.

package javaapplication19;

public class JavaApplication19 {

    static class MyRectangle {

        int x, y;

        int getArea() {
            return x * y;

    public static void main(String[] args) {
        MyRectangle r = new MyRectangle();      
        int area = r.getArea();


Series Navigation<< Java: Class
Java: Accessing object members >>

Leave a comment

Leave a Reply