// File: DynamicExample20.java // // Author: Rahul Simha // Created: Sept 2, 1998. // Modified: Sept, 2008. // // Can a static method access instance variables? class Customer { String custName; int bankBalance; public Customer (String cname, int balance) { custName = cname; bankBalance = balance; } public static int compare (Customer c1, Customer c2) { int b1 = c1.bankBalance; int b2 = c2.bankBalance; double ratio = (double) b1 / (double) b2; // Comparable if within same order of magnitude. if ( (0.1 < ratio) && (ratio < 10) ) return 0; else if (ratio < 1) return -1; else return 1; } } public class DynamicExample20 { public static void main (String[] argv) { Customer c1 = new Customer ("Dilbert D", 1000000); Customer c2 = new Customer ("Beetle B", 100000); int comp = Customer.compare (c1, c2); if (comp < 0) System.out.println ("c2 is richer"); else if (comp > 0) System.out.println ("c1 is richer"); else System.out.println ("same balance"); } }