Sunday, June 13, 2004

question: when I write an inernet enabled java program for my PC, my firewall (zone alarm) asks me if it is OK for javaw.exe to access the internet. How do I tell my firewall that I want that particular java program to access the internet but perhaps not other programs running under the java virtual machine?
11:16:53 PM    comment []
 Friday, May 21, 2004

"The only safe methods to call inside a constructor are those that are final in the base class "

There are cases where an instance method can be called before the constructor.  This gives odd results.  For example the following code:

public class SpecialFish extends Fish{
 public String species = "Walleye";
 void printSpecies(){
 System.out.println("the species is: "+species);
 } 
public static void main(String[] args) {
 SpecialFish sf = new SpecialFish();
 sf.printSpecies();
 } 
} 
class Fish {
 String defaultSpecies = "Carp";
 public Fish() {
 printSpecies();
 } 
 void printSpecies(){
   System.out.println("This is the species: "+defaultSpecies);
 } 
} 

You might expect the above to print "Carp" and then "Walleye",  but it prints "null" and then "Walleye"

The reason for this is that by default, Java waits until run-time to decide what object it is working on. 

Making a method final is the way to ensure that it is bound at compile time instead of run time.  Final is telling java that this method cannot be overridden.

Without making the printSpecies() method final, java has no clue if Fish or Special Fish will be using it, so it waits until run-time and then prepares (constructs) the base class so that it is ready to morph into any class, base or derived, that it might need to be.  In other words, it executes in the following order:

  1. the base-class constructors are called
  2. member initializers are called in the order of declaration
  3. the body of the derived class is called

So, the base-class constructor is called and inside that constructor you have a call to the printSpecies method.  Being a SpecialFish, the overridden printSpecies method gets called before the SpecialFish is even a twinkle in it's daddy's eye.  It's like asking a kid to take the SAT seconds before he is conceived.

So, the rule is, don't call a method that could be overriden from inside the constructor.


11:41:34 PM    comment []
 Tuesday, April 27, 2004

While every article about application development impresses upon us the importance of separating the data, logic and presentation layers,  MS Excel gleefully mixes the three. While this gets sneers from most developers, it is precisely the reason that so many people use it and love it and get themselves into all kinds of trouble with it... and then ask me to help. So I was very excited to get my copy of Excel Hacks today. Among other things, it has a lot of tips for separating the data from the formulas and the data from the presentation, making a complex Excel spreadsheet more maintainable.  Other things I was glad to see:
  • Chapter devoted to the SpreadsheetML, or the xml/xslt format used to describe spreadsheets. This facilitates generating spreadsheets using PHP and Java and other technologies that have nothing to do with Excel
  • Lots of hints for overcoming limits imposed by Excel: For example, override the number of undos allowed and override the limit to criteria for conditional formatting of cells.
  • Lots of easy to understand VBA code
  • Ways to recover data from corrupt workbooks
  • Long chapter on charts with some neato ideas for custom charts. I get asked a lot of questions about Excel charts and I never really learned more than basic charting skills.
  • And a question I have tried to solve several times, never getting a graceful solution: How to fill in empty cells with values
  • Solutions to pivot tables

I am working my way through each of the 100 hacks and in almost every one, I am saying to myself, "Gee, I didn't know excel could do that"  In summary, the O'Reilly hacks series has produced some excellent books and this one is one of the best.  It is clear, concise, error free, and doesn't assume you have the most recent version of Excel.  It is full of nuggets such as the fact that Excel purposely doesn't recognize that the year 1900 was a leap year (link blames it all on Lotus!).  Who knows, you might encounter that some day.


10:56:46 AM    comment []
 Tuesday, April 06, 2004

I wanted to remove a range of items from a list using the List.subList method. I tried the following:
	// al is defined previously as ArrayList al = new ArrayList();
        List l = al.subList(20,50);
        al.removeAll(l);

That got me the java.util.ConcurrentModificationException

The documentation says that a subList is dependent on the List that it is sublisting from. So, while a sublist is open, it is manipulating the parent list and seems to have a lock on it.   Instead, you can create a brand new ArrayList and fill it with the subList, thus freeing up the sublist so that a future call can modify the original List:

	ArrayList sl = new ArrayList(al.subList(10,40));
    	al.removeAll(sl);
		

So why does

 List = myList.subList(10,30);

get you a different animal than

ArrayList temp = new ArrayList(myList.subList(10,30)); ??

because in the first snippet, I haven't instantiated a new object, I just created a reference to an existing object.   In the second snippet, I created a brand new object.  That's what the word "new" gets me.


6:06:45 PM    comment []
 Monday, March 29, 2004

IntelliJ IDEA If you use IntelliJ 4.0 to check out projects from CVS make sure you click the "Change keyword substitution" to binary button in the CVS checkout procedure. Failure to do this could cause your project to fail to compile since the jar files will be corrupt.
3:03:37 PM    comment []

IntelliJ IDEA, the Java development tool that I am liking more and more, uses log4J to build a log file that is stored as : USER_HOME\.IntelliJIdea\system\log\idea.log

(On my windows installation, USER_HOME is C:\Documents and Settings\timcguir )

This file has lots of information about the internal workings of IDEA and has error messages that explained to me why the IDEA GUI Builder wasn't working like I expected.


11:19:57 AM    comment []
 Wednesday, March 17, 2004

 

1 exerting force by reason of weight alone without motion

2 showing little change

  • An early frustration in learning Java is the error:   non-static method foo( ) cannot be referenced from a static context
  • Perhaps you are trying to call a simple method of your class from the main() method.  The main method is a static context.  The quick solution is to instatiate your object with MyObject mo = new MyObject right inside your main method.  now you have an instance of MyObject  
  • A static variable or method is going to persist across all the objects instantiated.
  • Static means that you lose the use of the 'this' keyword because the use of 'this' implies that you have an instantiation of an object.
  • Static means that it doesn't depend on the instantiation of an instance of the object. It still exists and can be called from an instance of the object, but it can also be called without creating the object.
  • The main() method is static because you have to call it to start a java program. That is, you call it before any objects are instantiated.
  • Consider the following class: 
    public class AddMe{
    	
    	static int num = 100
    	
    	static int add(int i){
    		int sum = i+2;
    		return sum;
    	}
    }
    
  • When I want to access the variable num, I can just say, myNum = AddMe.num and get the number 100.
  • I can also instantiate AddMe am = new AddMe() and then call myNum = am.num with the same effect.
  • Same with the add method. myNum = AddMe.add(2) sets myNum to 4 and so does myNum = am.add(2)
  • During my one week java class from Sun, I completed an exercise by using all static methods. I could then call the methods just like I made function calls in PHP or VB. My solution was used as an example of bending Java to act like a procedural language. Which, in case you need to be told, was frowned upon. It avoids all the Object Oriented concepts of Java. You don't even ever need to instantiate a class!

6:55:17 PM    comment []
 Tuesday, March 16, 2004

My Findings on HashMaps:
  1. A Map is one type of Collection in Java. Collections are used to hold a bunch of objects. Different Collections let you look up your objects in different ways.
  2. The special thing about Maps is that the way you look up your objects in a Map is with another object.
  3. A Map is like an associative array in PHP. It is a container for key - value pairs.
  4. To get values out of a Map, you iterate through the Map's keys and use the get() method to get the value associated with that key back out.
  5. Map is the interface, Hashmap or TreeMap is the implementation of that interface, just like ArrayList is an implementation of List. So, to make a new HashMap, you would say Map myHashMap = new HashMap();
  6. A HashMap uses the hashCode() method available to all Java objects to take the hashCode of your object so that it can quickly find it when you want it.
  7. with a HashMap, your objects will not be sorted the way you expect
  8. with a TreeMap, the objects are sorted in a way that makes sense.

12:20:32 PM    comment []
 Monday, March 15, 2004

Here is the javascript function I used to disable the link that people often click more than once:

 function disable_links(){ 

// this function disables all the links on the page 

  for(var i=0; i < document.links.length;i++)

  {

      document.links[i].onclick=function () { return false; }

      document.links[i].title = "The exam has been submitted. Please wait for results page";

  }

  return true;

}


12:52:50 PM    comment []

For two weeks I had this really frustrating problem:

When users submitted the online fisheries exam, sometimes they would get an error page while the results would be emailed with no problems. I kept testing it and never got the error. I wondered if the database was crapping out because of too many open connections, I wondered about browsers caching error pages.

Then I watched someone submit the exam. They were like, "it sometimes takes several clicks before it submits"..... click click click. It is second nature to me not to submit a form more than once, no matter how long it takes the server to process the form.

 So, when testing, click all the buttons more than once, because it's a sure bet your users will.

In any case, there should be some code that handles this problem, but I didn't write the thing, I just inherited it.


9:39:39 AM    comment []
 Saturday, March 13, 2004

The Apache Software Federation now has Planet Apache, a set of web logs for their members.  (From Rogers Cadenhead)
8:41:23 AM    comment []

I had the hardest time using an ArrayList.  I had an ArrayList full of Fish objects and getting them out with ArrayList.get() and wanting to do some Fish type operations such as printing out data about the Fish and comparing it to another Fish.

To get the third Fish out of the ArrayList it is in, I tried

currentFish = myArrayList.get(2);

The trick is that ArrayLists (and other containers) store all objects as Objects instead of as Fish or Dogs or Cats or Cars or Strings.   This behavior happens for all types of java Collections.  So when you call an object back out of an ArrayList, you have to cast it back into whatever object it is supposed to be 

currentFish = (Fish)myArrayList.get(2);

Further confusion happens because every object has toString() over-ridden and automatically available so that when you think you are getting a string back from an ArrayList and want to print it out, it works fine.

I even had it print out the results of getClass() on the Object I was retrieving from myArrayList and even that told me it was a Fish!  But still, it was being treated like an Object until the cast.


7:57:53 AM    comment []
 Thursday, March 11, 2004