Tino Intro To Java

Last modified: January 07, 2024

Fields

Fields store data about an object or class. Instance variables (sometimes called attributes or properties) store the data that belongs an object, whereas static fields (sometimes called class variables) store data that belongs to the whole class.

Data Types

Every variable must be declared with a type. The type determines what kind of information is stored. A variable can store a single integer value, a boolean (true/false) value, a decimal value, or a reference to an object (an instance of a class). Data types that hold a single value are known as primitive data types. For now, you only need to know these kinds of primitive types:

int: Stores an integer value ranging from -231 to 231 - 1 (approximately -2 billion to +2 billion)

double: Stores a floating point decimal value ranging from approximately -1.7 x 10308 to 1.7 x 10308

boolean: Stores a true / false value

In addition to primitive types, a variable's type can also be a class. Since text is a common form of data, you should be aware of the String class that is designed to store text. Strings have special syntax in java, so you can create a string by simply putting text between double quotes, such as "Hello There". We will explore the String class later when we learn to use its methods to manipulate strings.

Hint: primitive types are all lowercase, but class names are always in UpperCamelCase.

Example

We will discuss class variables (static fields) later. For now, let's create a Person class with instance variables:

public class Person {

    // Instance Variables - fields storing info about the state
    //                      of an object (attributes/properties)

    int myAge;          // Age in years
    double myHeight;    // Height in meters
    boolean isStudent;  // True if enrolled at any school, false otherwise
    String myName       // Person's name

    // Remaining code not shown

}

Note that variables are named using lowerCamelCase

Declaration vs Initialization

So far we have only declared our instance variables. Declaring a variable means you are simply telling Java "Make a variable named ______ that can store data of a given data type", but you haven't actually told Java to store any data in the variable yet.

To actually create and store data, you need to initialize the variables.

For primitives, that just means assigning the variable a value, such as myHeight = 5.9;.

For objects, you need to set the variable to an instance of the appropriate class using a constructor to initialize the variable, such as cir = new Circle(5);.

  • Instance variables are commonly initialized in the object's constructors.
  • If an instance variable is declared and initialized in the same line, it will be equivalent to initializing it at the beginning of every constructor.
  • If you do not initialize an instance variable, it will be automatically be assigned a default value in the constructor.
    • The default value for primitive data types is 0 for number types such as int or double and false for boolean.
    • If the data type is a class, the default value is null (meanining the variable is not referencing any object).

Dark Mode

Outline