Everything is an Object*.
You'll be making them, you'll be interacting with them, everything you do programmatically will revolve around Objects.
Objects are defined by a Class.
A Java Class defines an Object. Mostly, each Class is a separate .java file, which 'defines' an Object. Classes are the specification, Objects are the implementation of this specification - the actual things you use.
It's very simple. Let's create our very first Class. Let's call it Person and consider it be the specification for a human person.
Create a file called Person.java and add this to it:
public class Person
{
public Person()
{
}
}
That's it. We now have a Person class, and if we wanted, we could create Person objects (more on that bit later).
We've create a class 'Person' using the 'public class Person' line. This means we're creating a class which is viewable wherever it lies within a Java package (where each file is stored in each underlying directory) and it's called Person.
We then encapsulate everything about that class by putting everything inside the curly brackets.
You'll then see this within this encapsulated block:
public Person()
{
}
This is a blank constructor. You need a constructor so that the class can be instantiated. Instantiation is the process of creating an Object from the Class, i.e. some usable Object.
You'll see from it's curly brace block that it doesn't currently do anything. But it does allow an instance of this class to be instantiated, i.e. 'created'.
In another class we could now do this:
Person dean = new Person();
'dean' would then be a person Object! We've created an Object specification (Person) and instantiated a real Object instance from it (dean).
*Assume this for now. Java has primitives, but for all intents and purposes, you'll only be programming to the Object model, and only using primitives.

No comments:
Post a Comment