turin said:
I tried to invoke the make.bat file, but I got errors... I put the most recent code (the stuff that we're currently discussing) into the lesson2.java file.
A file called lesson2.java must contain a class called lesson2. If the class inside is called Test, the file must be called Test.java. The names must be consistent.
- The programmer (me) can arbitrarily use most combinations of letters (strings; is there a term for the smallest part of code that is still code?) as variables in the source code. The frame in the source code is one such example.
Yes, variable names and method names are arbitrary combinations of alphanumeric characters and some special characters like the underscore. They also cannot begin with a number, only a letter. People have various conventions for style, and prefer names written in the style of
loopCounter to other possibilities like loop_counter and LOOPCOUNTER.
The compiler actually recognizes things like variable and method names based on their position in the code; for example, the keyword "class" is a reserved word in the Java language; you cannot use it for your own variable names. Whenever the compiler sees the word "class," it expects a properly-formatted class name (made up by the programmer) to follow it.
There are some combinations of letters that the compiler recognizes for a specific purpose. The JFrame in the source code is one such example.
Yes, there some special words that are used to define structures; these words are called
reserved words. A non-exhaustive list includes new, class, public, private, protected, if, while, for, continue, break, do, and so on.
Other words like JFrame are not actually built into the compiler; they are classes written by other people. When you use the word JFrame to indicate the type of a variable (again, the compiler knows it's used as a variable type by its context in the code), the compiler goes out and looks for a class called JFrame.
Most of the combinations of letters in the source code that the compiler recognizes for a specific purpose require the programmer to alert the compiler's attention by declaring the import lines. If the import lines were not included, then the compiler would not recognize combinations of letters such as JFrame, and so such combinations of letters would also be arbitrary like frame.
That is correct; but please note the distinction between words the compiler recognizes internally (like class and for and if), versus words the compiler used to reference other code (like JFrame).
I suppose that learning the combinations of letters and the necessary imported classes that the compiler recognizes will just come with time/experience. Is there a book or (preferably) an online resource that lists these compiler recognized things?
The Java environment provides a wealth of classes that you can use to build your applications; JFrame is just one. The entire API (application program interface) can be viewed here:
http://java.sun.com/j2se/1.4.2/docs/api/
Look in the left-hand frame and view the entry for the JFrame class if you'd like. You might also want to check out some very useful and simple classes like Vector.
I don't see any kind of algorithmic "flow" in the source code; I don't know what else to call it. In Q-BASIC, FORTRAN, and Matlab, there is an algorithmic flow that I recognize.
There definitely still is flow control in Java; once execution enters the top of a method, for example, it runs through that method exactly as you're used to seeing it run in other languages. The flow control is complicated by object orientation, however; constructors are run whenever the "new" reserved word is used, for example. You'll get used to the flow control very quickly, don't worry. Once you know how it works, you'll think it's positively brilliant.
Test is a class, and JFrame is a class, but JFrame is inside Test (by inside, I mean that it follows the openning curly bracket after the class declaration but preceeds the next closing curly bracket). Is this like nesting?
I would not say that JFrame is
inside Test. JFrame actually has it's own quite long source code file, called JFrame.java, buried deep in the JDK library. I would simply say that Test
uses JFrame, by referencing it. This is how you build applications in Java; objects use other objects.
Is the line of the source code protected JFrame frame; like declaring a variable to be used named frame that is of the data type JFrame and that is protected from something?
Absolutely correct in every way. The variable is in fact protected from the outside world; only code within the Test class can modify it (I'm being a bit sloppy, but that's close enough). The reason people use protection is to enforce good programming habits. Think back to the ChessPiece class -- you wouldn't want any old code anywhere in the world to be able to just arbitrarily set the x and y coordinates to anything they want. Only certain x and y positions are allowed; (-1, 105) would not be a valid position. Instead of trying to make all the code in the entire world aware of those restrictions, you make the variables private, and write a move() method. The move method could verify that the given position is valid before updating the variables. This way, your ChessPiece class is protected from the blunders of other programmers -- or even your own blunders.
I still don't understand what you mean by a Test object being created. You say that the constructor is called automatically when the Test object is created, but then why do you have to type the constructor? Does typing public class Test { ... not constitute creating a Test object?
Typing "public class Test {" does
not create an object. You've just written a class -- a blueprint for an object. To actually create an object of the Test class, you use the "new" keyword, like "new Test()". When the "new" keyword is actually executed -- when the program is run -- the memory is allocated and so on to actually create a Test object.
Consider that classes exist in source code, and objects exist in memory at run-time.
Is the syntax A.B().C(some stuff) generally useful, or is it very specific to the JFrame class? This syntax seems to imply an operation of some kind. What is the function of the .B() in this construct? B() and C() seem to resemble mathematical functions of variables. Is there some relationship/significance to this?
Right on target. The JFrame class has variables and methods (remember, that's all a class is -- a collection of variables and methods that operate upon them). Let's say you have two windows, though, not just one. Maybe one is called mainWindow, and the other is called toolPalette. If you want to make one of these windows visible, you have to specify which one to the compiler. You do so by writing one of the following two lines:
mainWindow.setVisible(true);
toolPalette.setVisible(true);
The . loosely translates into "member." When you write mainWindow.setVisible, you are referring to the setVisible member of the mainWindow object. Both mainWindow and toolPalette are of class JFrame.
In the main class, you say we're going to create a Test object, but I thought we had already created one at the beginning of the source code.
Nope, once again, the beginning of the source code defines the blueprint for an object -- a class. A class is a blueprint for an object. You still have to actually use the blueprint to make an object, and that mechanism is the "new" keyword. Think of the "new" keyword as a construction crew which uses a blueprint, which defines what a house
is, to build a real house. You can write one blueprint, and the construction crew can go out and build lots of houses from the blueprint that all start identical. After their construction, however, they can be modified independently. One family can put a blue couch in their house, while another can choose a red one.
The JFrame class is a blueprint for a window; you can make lots of windows from that blueprint, and each one is independent. They can have different titles, different contents, and so on.
Let me know if anything is still confusing..
- Warren