Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

A class defines the blueprint for a data format, kind of like a template. They encapsulate data and the methods that operate on that data. Following the school example, you might have a Student class, a Course class, and a Teacher class.

Defining a class in Java involves several steps. Here's a basic example of how you can define a class in Java:

public class MyClass {
    // Fields (or attributes)
    private String name;

    // Constructor
    public MyClass(String name) {
        this.name = name;
    }

    // Method
    public void sayHello() {
        System.out.println("Hello, " + name);
    }

    // Getter and Setter methods
    public String getName() {
        return name;
    }

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

In the above Java class:

  • MyClass is the name of the class.

  • name is a private field (or attribute) of the class. It's private, which means it can only be accessed within the same class. No outside class can access this field directly. They have to use getter (getName()) and setter (setName()) methods.

  • MyClass(String name) is a constructor method. This is a special type of method that is used to initialize the object of the class. It's called when an object of the class is created.

  • sayHello() is a method of the class. This is a function defined inside the class that performs a certain action.

  • getName() and setName(String name) are getter and setter methods, used to read and modify the private variable name.

You would then create an object of this class and use its methods in another class, likely with a main method, like so:

public class Main {
    public static void main(String[] args) {
        MyClass myObject = new MyClass("World");
        myObject.sayHello();  // Outputs: "Hello, World"
    }
}

Demo - Person Class

package Demo1PersonClass;
/**
 * @author Dr. Kevin Roark
 * Class contains attributes and methods for a Person
 */


public class Person {
    /*  -----------------------------------
     * Class Variables
      ----------------------------------- */
    private String firstName;
    private String lastName;
    private int age;

    /*
     * Constructors
      ----------------------------------- */
    public Person(String firstName, String lastName, int age) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public Person() {
        super();
        this.firstName = "Unknown FirstName";
        this.lastName = "Unknown LastName";
        this.age = -1;
    }


    /* -----------------------------------
     * Getter/Setters
      ----------------------------------- */
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

	/*  -----------------------------------
	 * Class Methods
	   ----------------------------------- */
    /**
     * Function creates a string with labels and Object data
     * @return String
     */
    public String PrintInfo()
    {
        String myReturn = "";
        myReturn = "Name: " + this.getFirstName() + " " + this.getLastName() + "\n";
        myReturn += "Age: " + this.getAge();
        return myReturn;
    }
}

Driver

package Demo1PersonClass;

public class PersonDriver {
    public static void main(String[] args) {
        //Demo of using argument constructor
        Person myPersonOne = new Person("Kevin", "Roark", 61);
        System.out.println(myPersonOne.PrintInfo());

        //Demo of Default Constructor
        Person myPersonTwo = new Person();
        System.out.println(myPersonTwo.PrintInfo());

        //Now using the setters - give the default constructor some values
        myPersonTwo.setFirstName("Fred");
        myPersonTwo.setLastName("Flintstone");
        myPersonTwo.setAge(29);

        //now output the myPersonTwo that now has data
        System.out.println(myPersonTwo.PrintInfo());

    }
}

  • No labels