My learnings:
- The University class has a run method within it rather than in the main class as this will allow greater flexibility - eg if we require more than one university
- If a numerical field is not going to have any calculations done on it, make it a string.
- If you loop through an array, null records are returned - check if the array is not null, perform action, otherwise skip it and continue.
- Convention is to use capitals for the name of a variable that is declared as final (a value that does not change. I consider this to be static, however static has a different meaning in Java).
BEN'S NOTES
STATIC VS INSTANCE VARIABLES AND METHODS:Static = shared across all instances of the class. i.e. every object of that class can access the same variable.
so if private static int id=1000, if you have objects s1, s2, s3:
if s1 increments id, then s2 increments id, then id will equal 1002.
Static variable = shared across all instances of the class.
Instance variable = accessable only to that instance of the class
Static method = a method shared across all the instances of the class
you can access it from your driver, just by using the class name and a dot.
e.g. Student.getId();
Whereas an instance method can only be accessed by referencing that instance of the class
e.g. student103.getId();
THIS:
when 'this' is used in a constructor, it reference that instance of the class.
so when creating student s1:this.name references the variable 'name' of the s1 object
when creating student s2:this.name references the variable 'name' of the s2 object
ACCESSORS VS MUTATORS:
An accessor takes no parameters because it requires no input to complete it's code
A mutator returns nothing because it's function is to change, not to return information
Research MVC
Model, View, Control
Why we return a value from the toString method, and not just print out
While we're just using text-based applications, printing out from the toString method is fine, but it is not good if you're actually using a GUI. So returning the values is much more helpful for non-text based applications.
Student s1 = new Student();Student s1 tells the computer which memory address to use
new Student() instantiates the object and calls the constructor to set up an object of that class
AN ARRAY OF OBJECTSprivate Student[] students
this makes an array of objects of type Student, and the array is called students
ARRAYS
It is a good practice to use a constant to define the size of the array.
e.g.
private final static int MAX_NUM;
student = new Student[MAX_NUM];
ARRAYS IN METHOD RETURN VALUES AND PARAMETERS
as a return value: public Student[] getStudents ()
as a parameter: public Unit ( Student[] students )
USING .length WHEN LOOPING THROUGH AN ARRAY
.length gives you the number of how many items are in the array, so if one of those items is NULL, you're in trouble.
A good way to get around this is when looping through an array, check first if the item in the array is equal to NULL, and if it isn't, then go ahead with your action, otherwise, skip it and keep going in the loop.
Example:
public String getStudentsEnrolledDetails()
{
Student student = null;
String output = "";
for (int index=0; index < studentsEnrolled.length; index++)
{
student = studentsEnrolled[index];
if(student != null)
output += student.toString();
}
return output;
}
RESERVING A MEMORY LOCATION SEMANTICALLY CONNECTED TO A CLASS
You can instantiate an object over 2 lines. e.g.
Student s1;
s1 = new Student();
So you can reserve a place in memory just by: Student s1;
No comments:
Post a Comment