Keep current with the latest trends in technical communication by subscribing to the I'd Rather Be Writing newsletter. 4,500+ subscribers

Recent Comments

Search results

Java: Classes

Quick summary

a class is a template used for creating objects

must be initialized with an object to use

can be initialized with arguments

uses Upperclass camel case

class name must match file name

you can only have 1 public class per Java file, but you can have multiple non-public classes in the same file

contains fields (primitives or object references) and methods

Eclipse example

object_example

Example

publicclassDog{Stringbreed;intage;intsize;Stringcolor;voidbark(){}voidhungry(){}voidsleeping(){}}publicstaticvoidmain(String[]args){Dogd=newDog();// make a new objectd.bark();// call the bark method on my objectd.size=40;// set the size for the object}

Static classes

If a class is static, it means you don’t have to create an instance of the class.

On a class, the static keyword means that only one instance of that class exists. You can’t create a new instance of a static class
– Java 7 for Absolute Beginners

“Instantiating a class”

Instantiating a class is the same as creating an object from the class; also referred to as initializing a new object. You create an “instance” of the class.

All objects of a class have access to the methods and fields of that class.

How you create a new object

Constructors create the objects. The new keyword is the default constructor for classes. (You can use “construct” instead of “create”).

You use a constructor to construct the object from the class (template). The most common constructor is the new keyword, but there are other ways to instantiate a class as well.

You could also create an object reference without assigning any value to it. You might see something like this:

PointoriginOne;

In this case, Point is the class and originOne is a reference variable to the class. But until you invoke the new keyword, you haven’t actually created a new object here.

Fields

The variables declared inside a class are referred to generically as fields. However, you can be more specific with the terminology:

local variables: fields inside a method or constructor

instance variables: variables inside the class but not in any method. (They only become alive when you instantiate the class.)

class variables: variables within a class but with the static keywords

Questions to ask about a class

Is it public or private?

What fields and methods does the class have?

Does the class inherit another class?

What does the class do?

What does the class return?

How is the class instantiated?

Fields

A field can be an object reference:

Addressaddress;

Here address is the object that refers to the class Address. We haven’t instantiated the class yet until we invoke the class’s constructor. Here we just define the object reference. The class isn’t actually initialized.

Abstract versus concrete

Classes are by default concrete. If they’re abstract, you can’t instantiate them. Instead, you must extend them to create a subclass from them.

Stay current with the latest in tech comm

Keep current with the latest trends in technical communication by subscribing to the I'd Rather Be Writing newsletter. 4,500+ subscribers