Objects have:

  1. State
  2. Behaviour

“public class Main” is a statement that creates a class.

A class is a bit like a powerful user-defined data type.

Variables that you create in methods can’t be accessed outside of that code block: they are local to it. Variables that you declare in classes can. (Depending on whether it’s public etc?).

“Access modifier”. As a rule of thumb use private access in classes. You don’t have to do this in the methods we’ve done previously because they are private anyway.

Variables in a class are like its state. For example these variables describe the state of a car:

public class Car {

    private int doors;
    private int wheels;
    public String model;
    private String engine;
    private String colour;
}

In the main class, you can create new objects of the class car. Here there is a new porsche and new holden. You can then define the variables or the state of these objects. However of the types declared in the car class, you can only access model because that is public and the others are private.

public class Main {

    public static void main(String[] args) {
	    Car porsche = new Car();
	    Car holden = new Car();
        porsche.model = "Careera";
    }
}

If you create a method in a class, that is attached to all the objects you create for a particular class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s