Undefined Variables of a Class Type
In this lesson, we will look at what happens when you forget to initialize your variables.
We'll cover the following...
Two instances of the class Square
Let’s create two Square
objects using the class Square
given in the previous chapter. The following program attempts this task but includes a common mistake that anyone might make. Do you see the mistake?
Press + to interact
public class DemoTwoSquares{public static void main(String[] args){Square smallSquare, bigSquare;smallSquare.setSide(1);bigSquare.setSide(100);System.out.println("Small square: side = " +smallSquare.getSide() + ", " +"area = " + smallSquare.getArea());System.out.println("Big square: side = " +bigSquare.getSide() + ", " +"area = " + bigSquare.getArea());} // End main} // End DemoTwoSquares
You should receive the ...