Java Classes

Classes:

package main.java; public class MyClass { public int myMemberVar1 = 3; public String myMemberVar2 = "test"; public void myMethod1 () { System.out.println( "Var 1 is " + myMemberVar1 + " Var 2 is " + myMemberVar2 ); } }

package main.java; public class MainClass { public static void main (String [] args) { MyClass myObject = new MyClass(); myObject.myMemberVar1 = 5; myObject.myMethod1(); } }

- A class defines a new complex variable type which can have its own variables and functions to access

- Variables defined in a class are called "member variables", and functions defined there are called "methods"

- When you create a variable of this type, it is called an Object

- An object has the variables and functions defined in the class, and they can be accessed with the object name followed by a period followed by the method/variable name

- If you replace "public" with "private" before the method or member variable, it will only be able to be accessed from within a member function definition

- If you pass an object as a function parameter value, changes made to the object within the function will remain changed out of the function

Simple Getters and Setters:

package main.java; public class MyClass { private int myMemberVar1 = 3; public int getMyMemberVar1 () { return myMemberVar1; } public void setMyMemberVar1 (int myParam1) { myMemberVar1 = myParam1; } }

- It is best practice to keep member variables private and to modify or retrieve them through functions, typically called getters and setters because of what they do

- This allows you to add code such as to ensure that invalid data is not set, or that the value being retrieved isn't an empty variable (see next example)

- By convention, the getter and setter functions will be named with "get"/"set" followed by the function name starting with an uppercase letter

Practical use of Getters and Setters:

package main.java; public class MyClass { private int myMemberVar1; // Before a value is assigned, value is null public int getMyMemberVar1 () { if (myMemberVar1 != null) { return myMemberVar1; } else { return 0; } } public void setMyMemberVar1 (int myParam1) { if (myParam1 > 0) { myMemberVar1 = myParam1; } } }

Basic Constructor:

package main.java; public class MyClass { private int myMemberVar1; public MyClass () { // Basic Constructor with no parameters this.myMemberVar1 = 3; } }

- A constructor is a function that is executed when you create an object (in this case, using the "= new MyClass()" function )

- It must have the same name as the class

- It is useful for setting the start values for the member variables, especially complex variables that can't get a value simply with the "=" operator

- "this." means that it is referring to the member variable in this class (so as not to be confused with other variables with the same name)

Alternative Constructors:

package main.java; public class MyClass { private int myMemberVar1; public MyClass () { this.myMemberVar1 = 3; } public MyClass (int myParam) { // Takes a parameter this.myMemberVar1 = myParam; } }

- You can make alternative constructors distinguishable by receiving different parameters

- In the example above, the second constructor would be used by a call with an integer passed, e.g. MyClass myObj = new MyClass(12);

- If you define a constructor with parameters and want to also be able to access a constructor without parameters, you must also define the constructor without parameters (which otherwise would have existerd by default)

Copy Constructors:

package main.java; public class MyClass { private int myMemberVar1; private String myMemberVar2; public MyClass ( MyClass myObjToCopy) { this.myMemberVar1 = myObjToCopy.myMemberVar1; this.myMemberVar2 = myObjToCopy.myMemberVar2; } }

- Copy constructors allow you to create a new object with the same values of an existing object that gets passed as a parameter

- When making a copy of an object, do not try to give it values with the = operator (e.g. myObj1 = myObj2;), that will cause both objects to reference the same data (so modifying one would modify both)

Imported Classes:

package main.java; import java.util.ArrayList; // Use the import keyword, then name the external class public class MainClass { public static void main (String [] args) { ArrayList<String> myList = new ArrayList<String>(); // ... } }

- For a class available from the internet (or for your own classes defined in a different package), you must import them in order to use them

Challenge

Create a class called "Dog" that stores its name, weight, breed, and whether it is neutered; create getters and setters for its variables, and create a single function that will display all of the details. Create a constructor that takes parameters for each of the variables, and a constructor that takes none. If a getter is called for data that is not filled, return a default value of 0, false, or "none".

In the main class, create two instances/objects of this class (one with the parameters and one without). For the object created without parameters, call a getter to ensure that the default value is returned, then set the values with the setters, and print the values accessed with the getters. Finally, run the display function for the object created with parameters.

Quiz

Completed