Skip to content
Now Accepting Applications for 2027-2028Apply
ITMagicITMagicAcademy
All topics
AP Computer Science AUnit 1 · Using Objects and Methods

Objects, Constructors, and Calling Methods

Distinguish classes from objects, create objects with constructors, and call void and non-void, static and instance methods with correct arguments.

Classes and objects

A class is a blueprint; an object is an instance created from it. A variable of a class type holds a reference to an object, or null.

Constructors

Objects are created with new and a constructor:

Rectangle r = new Rectangle(3, 5);

A class can have several constructors with different parameter lists (overloading). The arguments must match a constructor's parameters in number, order and type.

Calling methods

  • Instance methods are called on an object: r.getArea().
  • Static methods are called on the class: Math.sqrt(16).
  • A void method performs an action and returns nothing; it is used as a statement.
  • A non-void method returns a value that should be used: int area = r.getArea();

Method signatures

public int getArea()
public void setWidth(int w)

The signature tells you the return type, the name and the parameters. Calling r.setWidth(4.5) does not compile because a double cannot be passed to an int parameter.

null and NullPointerException

Calling a method on a variable that is null throws a NullPointerException.

Application program interfaces

On the exam you read documentation (an API) for a class you have never seen and use its constructors and methods correctly.

Quick summary

  • new ClassName(args) creates an object.
  • Instance methods use an object; static methods use the class name.
  • Use the return value of non-void methods; arguments must match parameters.

Topic quiz

5 questions. You need at least 80% correct to pass; passing unlocks the next topic.

Log in to take the quiz and save your progress. Creating an account is free.

Log in and take the quiz