Creating Your Own Classes
Creating Your Own Classes
1. The name of the class should be meaningful, don't just call your class
XYZ or any random names you can think of.
3. The filename of your class should have the SAME NAME as your public
class name.
Declaring Attributes
To declare a certain attribute for our class, we write,
<modifier> <type> <name> [= <default_value>];
For example, you don't want to have a data type int for a student's
name, or a String for a student's grade.
name - String
address - String
age - int
math grade - double
english grade - double
science grade - double
average grade - double
Declaring Attributes
Since we want these attributes to be unique for each object (or for each student), we
should declare them as instance variables.
For example,
public class StudentRecord
{
private String name;
private String address;
private int age;
private double mathGrade;
private double englishGrade;
private double scienceGrade;
private double average;
//we'll add more code here later
}
where,
private here means that the variables are only accessible within the class.
Other objects cannot access these variables directly. We will cover more about
accessibility later.
Coding Guideline:
1.Declare all your instance variables on the top of the class declaration.
2.Declare one variable for each line.
3. Instance variables, like any other variables should start with a SMALL letter.
4. Use an appropriate data type for each variable you declare.
5.Declare instance variables as private so that only class methods can access
them directly.
Class Variables or Static Variables
For example:
public class StudentRecord
{
private String name;
..
public String getName(){
return name;
}
}
where,
public - means that the method can be called from objects outside the
class
String - is the return type of the method. This means that the method
should
return a value of type String
getName - the name of the method
() - this means that our method does not have any parameters
The statement,
return name;
in our program signifies that it will return the value of the instance variable name to
the calling method.
Another example of an accessor method is the getAverage method,
public class StudentRecord
{
private String name;
..
public double getAverage(){
double result = 0;
result = ( mathGrade+englishGrade+scienceGrade )/3;
return result;
}
}
Mutator Methods
- is usually written as set<NameOfInstanceVariable>.
Now let's take a look at one implementation of a mutator method,
public class StudentRecord
{
private String name;
..
public void setName( String temp ){
name = temp;
}
}
where,
public - means that the method can be called from objects outside the
class
void - imeans that the method does not return any value
setName - the name of the method
(String temp) - parameter that will be used inside our method
The statement,
name = temp;
assigns the value of temp to name and thus changes the data inside the
instance variable name.
Take note that mutator methods don't return values. However, it contains some
program argument or arguments that will be used inside the method.
Multiple Return statements
For example, consider the method,
public String getNumberInWords( int num ){
String defaultNum = "zero";
if( num == 1 ){
return "one"; //return a constant
}
else if( num == 2){
return "two"; //return a constant
}
//return a variable
return defaultNum;
}
Static methods
public - means that the method can be called from objects outside the class
static - means that the method is static and should be called by
typing,[ClassName].[methodName]. For example, in this case, we call the method
StudentRecord.getStudentCount()
int - is the return type of the method. This means that the method should
return a value of type int
getStudentCount - the name of the method
() - this means that our method does not have any parameters
Coding Guideline:
3. Always provide documentation before the declaration of the method. You can
use javadocs style for this. Please see example.
Sample Source Code for StudentRecord
class
Here is the code for our StudentRecord class,
public class StudentRecord
{
private String name;
private String address;
private int age;
private double mathGrade;
private double englishGrade;
private double scienceGrade;
private double average;
private static int studentCount;
/**
* Returns the name of the student
*/
public String getName(){
return name;
}
/**
* Changes the name of the student
*/
public void setName( String temp ){
name = temp;
}
// other code here ....
/**
* Computes the average of the english, math and science
* grades
*/
public double getAverage(){
double result = 0;
result = ( mathGrade+englishGrade+scienceGrade )/3;
return result;
}
/**
* returns the number of instances of StudentRecords
*/
public static int getStudentCount(){
return studentCount;
}
}
Now, here's a sample code of a class that uses our StudentRecord class.
Anna
Student Count = 0
The this reference
NOTE: You can only use the this reference for instance variables and NOT static
or class variables.
Declaring Constructors
- are important in instantiating an object. It is a method where
all the initializations are placed.
4. You cannot call a constructor directly, it can only be called by using the new
operator during class instantiation.
public StudentRecord(){
//some initialization code here
}
public StudentRecord(String temp){
this.name = temp;
}
public StudentRecord(String name, String address){
this.name = name;
this.address = address;
}
public StudentRecord(double mGrade, double eGrade, double sGrade){
mathGrade = mGrade;
englishGrade = eGrade;
scienceGrade = sGrade;
}
Using Constructors
To use these constructors, we have the following code,
public static void main( String[] args )
{
//create three objects for Student record
StudentRecord annaRecord=new StudentRecord("Anna");
StudentRecord beahRecord=new StudentRecord("Beah",
"Philippines");
StudentRecord crisRecord=new
StudentRecord(80,90,100);
//some code here
}
A good location to modify and increment the value of studentCount is in the
constructors, because it is always called everytime an object is instantiated.
For example,
public StudentRecord(){
//some initialization code here
studentCount++; //add a student
}
1: public StudentRecord(){
2: this("some string");
3:
4: }
5:
6: public StudentRecord(String temp){
7: this.name = temp;
8: }
9:
10: public static void main( String[] args )
11: {
12:
13: StudentRecord annaRecord = new StudentRecord();
14: }
Given the code above, when the statement at line 13 is called, it will call
the default constructor line 1. When statement in line 2 is executed, it will then call
the constructor that has a String parameter (in line 6).
There are a few things to remember when using the this constructor call:
1. When using the this constructor call, IT MUST OCCUR AS THE FIRST
STATEMENT in a constructor
For example, if you want to use the class Color inside package awt,
you have to type the following,
import java.awt.Color;
import java.awt.*;
This is done by using the package name to declare an object of a class.
java.awt.Color color;
Creating your own packages
To create our own package, we write,
package <packageName>;
Copy all the classes that you want to belong to this package inside this
folder. After copying, add the following code at the top of the class file. For
example,
package schoolClasses;
public class StudentRecord
{
private String name;
private String address;
private int age;
..
Access Modifiers
There are four different types of member access modifiers in Java: public,
private, protected and default. The first three access modifiers are explicitly written
in the code to indicate the access type, for the fourth one which is default, no
keyword is used.
default access (also called package accessibility)
This specifies that only classes in the same package can have access to
the class' variables and methods.