My notes on Java
Tips
Conventions
- class names begin with a capital letter
- constants are all CAPS (final means no changes/compiler check)
Default values
- numeric types = zero
- char = '\u000'
- class references or references to arrays = null
A class file contains object definitions and methods:
class
|
---- Variables
|
---- Methods
- static variables are shared across the class
- instance variables are for each instance of the object
- public static void main() is the first executed object
modifiers = public, static, final, abstract, native, private, protected, synchronized
types are
value_type = void, integer, double, string, array, etc.
[Note: I'm not sure if it's KIOVI editor or this module that causes the extra spacing for this code to go away upon submission, but it's annoying.]
/**
* This is a special doc comment.
* The javadoc program can yank these to create online
* documentation (notice the ** at the start)
*/
/*
* This is a standard set of comments
*/
ClassName varname = new ClassName(); // how to define a new classed variable
modifier class ClassName {
static var = null;
vartype varname = null;
//method definition (return_type can be any - void returns nothing)
//static allows to get to the method even if the values are null
modifiers type methodName ( [type arg1], [type arg2], ..., [type argn]) {
//Executable code here
return return_value; //or return; for voids
}
}
class Sphere {
static final double PI = 3.141592638; // Class variable that has a fixed value
static int count = 0; // Class variable to count objects
// Instance variables
double radius; // Radius of a sphere
double xCenter; // 3D coordinates
double yCenter; // of the center
double zCenter; // of a sphere
// Static method to report the number of objects created
static int getCount() {
return count; // Return current object count
}
// Instance method to calculate volume
double volume() {
return 4.0/3.0*PI*radius*radius*radius;
}
// Instance method to change the radius
// Change the instance variable to the argument value void
changeRadius(double radius) {
this.radius = radius;
}
// Plus the rest of the class definition...
}
Threads and Conversations
Some excellent info...