Working with objects
Rectangle dashedRectangle = new Rectangle();
Rectangle solidRectangle = new Rectangle();
...
new class-name ([argument 1[, argument 2] ...] )
- Wording examples:
-
-
“Create an instance of class
Rectangle
”. -
“Create a
Rectangle
object.” -
“Create a
Rectangle
”.
-
Rectangle dashedRectangle = new Rectangle();
dashedRectangle.width = 28;
dashedRectangle.height = 10;
dashedRectangle.hasSolidBorder = false;
Syntax accessing object attributes:
variable.attributeName = value;
|
Exception in thread "main" java.lang.NullPointerException
at de.hdm_stuttgart.mi.classdemo.App.main(App.java:17) |
Rectangle r;
... // possible object assignment to variable r.
if (null == r) {
System.out.println("No rectangle on offer");
} else {
System.out.println("Width:" + r.width);
}