Defining and Using Classes

Defining Classes

  • Every method is associated with some class.

  • To run a class, we must define a main method.

public class Dog {
    public static void makeNoise() {
        System.out.println("Bark!");
    }
}

The code above can't be run directly because there's no main method.

public class DogLauncher {
    public static void main(String[] args) {
        Dog.makeNoise();
    }
}

However, the method could be called from another class.

$ java DogLauncher
Bark!

Object Instantiation

  • Classes can contain not just methods, but also data, such as the size of each dog.

  • Classes can be instantiated as objects. For example, we will create a single Dog class, and then create instances of this Dog.

Here's a Dog class which provdes a blueprint of Dog objects.

Array of Objects

To create an array of objects:

  • First use the new keyword to create the array.

  • Then use new again for each object that you want to put in the array.

Staitc vs. Non-static

Key difference between static and non-static methods:

  • Static method are invoked using the class name.

  • Instance methods are invoked using an instance name.

  • Static method can't access instance variables.

    , but are more simple.

Sometimes, a class may contain both static and non-static methods.

We could declare static variables which are properties shared by all instances of the class.

  • A variable or method defined in a class is also called a member of that class.

  • Static members are accessed using class name.

  • Non-static members cannot be invoked using class name.

  • Static methods must access instance variables via a specific instance.

public static void main(String[] args)

  • public: So far, all of our methods start with this keyword.

  • static: It is a static method, not associated with any particular instance.

  • void: It has no return type.

  • main: This is the name of the method.

  • String[] args: This is a parameter that is passed to the main method.

For example, this program prints out the first command line argument.

Using Libraries

In CS 61B, we will use libraries include:

  • The built-in Java libraries such as Math, String, Integer, List, Map.

  • The Princeton standard library such as StdDraw, StdAudio, In.

Last updated