Monday, September 22, 2008

Java Language Basics

I was a bit down on the Concepts trail. Not so language basics. This one read just fine. I guess the nuts and bolts of all programming languages have to have aspects in common, because at the end of the day they all have to perform similar tasks.

If I have a criticism it is not of what is there so much as what I feel is missing from the page on variables.

When learning VB there are whole chapters on the life of variables, but these Java tutorials don't seem to mention the concept explicitly. I posted a question on the subject in the tutorial discussion forum, but I was not really happy with the replies.

I'll set out the problem again here. Essentially I had used NetBeans to create what in VB is called a form but here seems to be called a JFrame. The Jframe had a button a text field and labels. I coded the button to put a string in one of the labels. So far so good. But I wanted the button to remember what it had done after the event code had finished executing. I wanted the button to store information in a variable (or field) defined by me.

According to my understanding of the theory set out in the Java tutorial, the button is an object belonging to a class, and it has a number of fields, defined by the class. And in the NetBeans IDE I could see these fields listed in the properties window. But what scope do I the user have to add fields to this list, and how do I do it?

In VB a form has code associated with it, the user can declare variables in that code, and any object on the form can use those variables, and the variables stay alive for as long as the form. This is very handy. And since VB is a widely used language, I think the Java tutorials might make explicit reference to this, and then compare and contrast the rules in the two language.

I tinkered around for hours but I could not apply this VB style variable declaration in a JFrame.

In the end I created and compiled my own class with all the "fields" I needed, and then put an object from that class on my JFrame. Actually getting that to work was also something of a nightmare, but I shall describe that in a later blog, which focuses on the Swing tutorial.

No comments: