Java: Object creation

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();      
        
        r.x=10;
        r.y=5;
        
        int area = r.getArea();
        System.out.println(area);      
    }
}

class

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