Working with Variables

Data types come into play when you need to declare variables to hold values that are important to your program. Variables can be either primitives or reference types. They are always declared within a class but a variable might be associated with the class as a whole or its scope might be limited to a method or a block of statements within a method.

Declaring a Variable

Here's a simple example of declaring an integer variable:

int maxLoginAttempts = 3;

When you create a variable in Java, you must know a few things:

  • You must know what data type you are going to use. In this case, that was the int primitive.

  • You must know what you want to call the variable (maxLoginAttempts).

  • You might also want to know the value with which ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.