|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
static keyword, an open brace character, initialization code, and a close brace character.
These tips and cautions will help you write better programs and save you from agonizing over error messages produced by the compiler.
javap program. That program generates a disassembly of Java class files.
Java's standard class library contains many examples of class block initializers. In addition to Java Database Connectivity's
(JDBC) use of that feature, Object uses a class block initializer to register its native methods (such as clone(), getClass(), and hashCode()) with the JVM. The following Object code fragment takes care of that task:
private static native void registerNatives ();
static
{
registerNatives ();
}
And what is a native method? Keep reading Java 101 to find out.
Trace the initialization process through the Employees source code below. You should end up with a numbered list that includes all initialization steps until the last Accountant object finishes initializing.
Employees.java
// Employees.java
class Employee
{
private String name;
private double salary;
static int count;
static double bonus;
Employee (String name, double salary)
{
this.name = name;
this.salary = salary;
if (this instanceof Accountant)
this.salary += bonus;
}
static
{
// Pretend to load bonus from a database.
bonus = 500.0;
}
{
if (count > 5)
bonus = 0.0;
count++;
}
String getName ()
{
return name;
}
double getSalary ()
{
return salary;
}
}
class Accountant extends Employee
{
Accountant (String name, double salary)
{
super (name, salary);
}
}
class Employees
{
public static void main (String [] args)
{
String [] names =
{
"John Doe",
"Jane Smith",
"Jack Jones",
"Bob Smyth",
"Alice Doe",
"Janet Jones"
};
double [] salaries =
{
40000.0,
50000.0,
30000.0,
37500.0,
52000.0,
47000.0
};
for (int i = 0; i < names.length; i++)
if (i < 3)
{
Employee e = new Employee (names [i], salaries [i]);
System.out.println ("Name = " + e.getName ());
System.out.println ("Salary = " + e.getSalary ());
}
else
{
Accountant a = new Accountant (names [i], salaries [i]);
System.out.println ("Name = " + a.getName ());
System.out.println ("Salary = " + a.getSalary ());
}
}
}
Last month, I issued a challenge quiz as a way of reviewing material presented in the "Object-Oriented Language Basics" series. The following three entrants emerged as the winners: