To be honest, we're now getting to the point where you'll want to start coding this stuff yourself.
Best bet - go grab IDEA InteliJ from here, and the Java Developer Kit (JDK) from here (the second one from the top title 'Java SE Development Kit (JDK)').
Install the JDK first into the default location, then IntelliJ.
Create a new project, from scratch in IntelliJ. Call it Test. Keep all other screens to the default settings until done.
IntelliJ should detect the JDK, check it has by going to 'File', 'Settings' then under 'Project Settings' there is 'Project Settings' (again). Click on 'JDK' under 'Platform Settings'. If your JDK isn't set, click on the + button and point to where you installed the JDK, probably something like: c:\program files\java\JDK1.6.0_06
Et voila, you have an Integrated Development Environment setup for developing Java.
Friday, April 10, 2009
Instance Variables
Each Object instance, i.e. every person (tom, dick, harry) you create from the Person class specification, can have its own instance variables.
Variables are things that can change. Ones that are stored and encapsulated within an Object are called instance variables, because there's one variable per instance.
We can change our Person Class to look like this:
You can see it's quite different to what we previously had.
Firstly, you can see two new instance variables we've added - 'name' and 'birthPlace'. Both are of type 'String' which is an inbuilt Object which holds letters and characters.
We defined them within the Class by using the private package modifier, meaning only methods within this class can have access to them. No other Object can touch them directly, if they were public, they could.
You'll see we changed the 'signature' of the constructor to also take these two new variables.
What this means is that when we create a Person Object we can pass two Strings in and these can get assigned to the private instance variables.
We do this by using the keyword 'this': this.name = name means that the value of name which has been passed into the constructor (the right hand side 'name') is being assigned to the Object's instance variable (the left hand side 'this.name').
i.e. we could create a dean Person instance by doing this:
The Object instance 'dean' now has it's instance variables assigned the values Dean and Crawley (respectively!).
Variables are things that can change. Ones that are stored and encapsulated within an Object are called instance variables, because there's one variable per instance.
We can change our Person Class to look like this:
public class Person
{
private String name;
private String birthPlace;
public Person(String name, String birthPlace)
{
this.name = name;
this.birthPlace = birthPlace;
}
}
You can see it's quite different to what we previously had.
Firstly, you can see two new instance variables we've added - 'name' and 'birthPlace'. Both are of type 'String' which is an inbuilt Object which holds letters and characters.
We defined them within the Class by using the private package modifier, meaning only methods within this class can have access to them. No other Object can touch them directly, if they were public, they could.
You'll see we changed the 'signature' of the constructor to also take these two new variables.
What this means is that when we create a Person Object we can pass two Strings in and these can get assigned to the private instance variables.
We do this by using the keyword 'this': this.name = name means that the value of name which has been passed into the constructor (the right hand side 'name') is being assigned to the Object's instance variable (the left hand side 'this.name').
i.e. we could create a dean Person instance by doing this:
Person dean = new Person("Dean", "Crawley");
The Object instance 'dean' now has it's instance variables assigned the values Dean and Crawley (respectively!).
Classes and Objects
Let's start at the very beginning of everything Java.
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:
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:
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:
'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.
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.
An introduction to Java
Maybe this will be of use to some, especially for those who read the RTDL2 blog.
I'm planning to bring you a very basic over-view of Java, probably a post a week or so, which should enable you to grasp the concept of Object Orientation utilising Java.
Not everything I say will utilise the correct nonclementure. Not everything I say will be perfectly formed, nor will it read like a text book or formal tutorial. It's intended for human consumption, for people with relatively little OO experience. It's going to be bloody basic - at least to begin with.
In my early days I found too many books that skimmed over the basics, meaning you simply couldn't grasp the larger concepts.
I hope my fellow RTDL2 blogger will keep tabs and ask any questions I may have omitted, I also hope it's of use for others, or it makes the whole exercise a waste of time.
I'm planning to bring you a very basic over-view of Java, probably a post a week or so, which should enable you to grasp the concept of Object Orientation utilising Java.
Not everything I say will utilise the correct nonclementure. Not everything I say will be perfectly formed, nor will it read like a text book or formal tutorial. It's intended for human consumption, for people with relatively little OO experience. It's going to be bloody basic - at least to begin with.
In my early days I found too many books that skimmed over the basics, meaning you simply couldn't grasp the larger concepts.
I hope my fellow RTDL2 blogger will keep tabs and ask any questions I may have omitted, I also hope it's of use for others, or it makes the whole exercise a waste of time.
Subscribe to:
Comments (Atom)
