View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Declare a class

Each public class is declared in a file with the name of the class followed
by ".java". Inside of a class definition body are the fields, constructors, and
methods.


To declare a class in Java use:


public class NameOfClass {
// fields
// constructors
// methods
}

or

public class NameOfClass
{
// fields
// constructors
// methods
}

Example:

public class Person
{
// fields
private String firstName;
private String lastName;

// constructors
public Person () {}

public Person(String theFirstName, String theLastName)
{
this.firstName = theFirstName;
this.lastName = theLastName;
}

// methods
public String getFirstName() { return this.firstName; }

public String getLastName() { return this.lastName; }

public void setFirstName(String name) { this.firstName = name; }

public void setLastName(String name) { this.lastName == name; }

public String toString() { return this.firstName + " " + this.lastName; }
}


Link to this Page