O'Reilly logo

Sams Teach Yourself Java™ in 24 Hours, Sixth Edition by Rogers Cadenhead

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Class Methods and Variables

At the insistence of our attorney, the next project is not the creation of a working virus. Instead, you create a simple Virus object that can count the number of Virus objects that a program has created and report the total.

Choose File, New File in NetBeans and create a new Empty Java File called Virus. Enter Listing 11.1 in the source editor.

Listing 11.1. The Full Text of Virus.java

 1: public class Virus { 2:     static int virusCount = 0; 3: 4:     public Virus() { 5:         virusCount++; 6:     } 7: 8:     static int getVirusCount() { 9:         return virusCount;10:     }11: }

Save the file, which NetBeans compiles automatically. This class lacks a main() method and thus cannot be run directly. To test ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required