Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

What

...

is

...

a

...

class?

...

In

...

Java,

...

all

...

code

...

is

...

associated

...

with

...

a

...

class.

...

Classes

...

have

...

fields

...

and

...

methods

...

.

...

Here

...

is

...

an

...

example

...

of

...

two

...

simple

...

classes:

{:=
Section
border
false
Column

} {column}{column} {column:width=500px} {code:title=Cat.java| linenumbers}
Column
width500px
Code Block
1 linenumbers
titleCat.java

public class Cat{
  private int age;
  private String name;

  public Cat(String name, int age){
    this.name = name;
    this.age = age;
  }

  public String getName(){
    return name;
  }

  public String sayHello(){
    return "Hello World!  My name is " + name 
        + " and I am " + age + " years old.";
  }
}
{code} {column:width=10px}{column} {column:width=500px} {code:title=CatsTheMusical.java}
Column
width10px

Column
width500px
Code Block
titleCatsTheMusical.java

public class CatsTheMusical{
  public static void main(String[] args){
    Cat mist = new Cat("Mr. Mistoffelees",8);
    System.out.println(mist.sayHello);
  }
}
{code} {column} {column}{column} {section} Lets make sure we understand how this works. First, lets talk about the contents of the file {{Cat.java}}. * _Fields_: The class {{Cat}} has two fields, {{name}} and {{age}}. Each field has a _type_, which ensures that we can only store a {{String}} in {{name}} and an {{int}} in age. The word {{private}} before each field indicates that the field can only be accessed from within the class {{Cat}}. In Java, by convention most fields are private, and if you want to be able to access them from outside the class, you add _getter_ and _setter_ methods (we will get to these shortly). * _Constructors_: The class {{Cat}} has a single constructor {{public Cat(String name, int
Column

Lets make sure we understand how this works. First, lets talk about the contents of the file Cat.java.

  • Fields: The class Cat has two fields, name and age. Each field has a type, which ensures that we can only store a String in name and an int in age. The word private before each field indicates that the field can only be accessed from within the class Cat. In Java, by convention most fields are private, and if you want to be able to access them from outside the class, you add getter and setter methods (we will get to these shortly).
  • Constructors: The class Cat has a single constructor {{public Cat(String name, int age){...}

...

  • }}.

...

  • Constructors

...

  • are

...

  • used

...

  • to

...

  • create

...

  • instances

...

  • of

...

  • the

...

  • class. Notice that:
    • Because the constructor is public, it can be invoked from other classes. An example of the invoking the constructor can be seen in CatsTheMusical.java.
    • We use the keyword this to set the variables name and age. The arguments of the constructor create variables with the same name as the the fields of the class, shadowing the fields. By using the keyword this, we can access methods and fields associated with the current instance of the class (being built in the constructor). This pattern, while a little confusing, is conventional.
  • Methods:

Here, we included a method getName() in class Cat.