Welcome to Java Class: Learn Java One Bit at a Time

In summary, in this conversation, the speaker introduces the concept of Java and explains that to make use of Java programs, a compiler and a runtime environment are needed. The compiler converts human-readable source code into machine-readable object code, while the runtime environment executes the bytecode. The speaker recommends downloading the Java J2SE 1.4.2 SDK and explains how to compile and run a simple Java program using the command line. The terms "program," "object code," and "machine code" are discussed, as well as the purpose of a compiler and the limitations of a physical processor.
  • #1
chroot
Staff Emeritus
Science Advisor
Gold Member
10,295
41
Welcome to Java Class!

We're going to learn a bit of Java here, one bit at a time, using real world examples to understand what we're doing. My aim is that all of the Java posted here is executable, so you can try it yourself.

First, before you can make use of any Java programs, you'll need two things: a compiler and a runtime environment. The compiler essentially just converts your text source files into a stream of bytes that can be executed directly by a physical processor, which doesn't have too many smarts. You could open up a hex editor and type in the actual bytes, but that would be very, very difficult! That's why compilers exist: to convert a human-readable program into a dumb-machine-readable one. The human readable file is called source code, and the machine-readable translation is called object code.

The second part, the runtime environment, is specific to Java. Unlike other languages, your Java code is not compiled for a specific physical processor, like a PowerPC G5 or a Pentium III. Instead, your Java code is compiled to neutral bytecode. This neutral bytecode resembles the PowerPC G5 and Pentium III instruction sets, but is not run directly. Instead, the bytecode is executed by a program that "pretends" to be a physical processor -- it's called a virtual machine.

Okay, so where do you get these programs? Here: http://java.sun.com/j2ee/1.4/download.html

You want the Java J2SE 1.4.2 SDK (Java 2, Standard Edition, version 1.4.2, Software Development Kit). The filename, if you're on Windows, is j2sdk-1_4_2_04-windows-i586-p.exe.

So, download and install that. When you're done, we'll be ready to compile and run our first program.

- Warren
 
Technology news on Phys.org
  • #2
Okay, so now you've run Sun's fancy installer, and it's all done. What now?

Look in this directory on your hard drive: C:\j2sdk1.4.2_04

The most important stuff for you to know is in the "bin" directory, which means "binary," or "executable programs."

Notably, in the C:\j2sdk1.4.2_04\bin directory, you'll find two programs that will be most useful to us: java.exe, and javac.exe.

java.exe is the runtime environment, which runs Java programs.

javac.exe is the compiler, which compiles Java programs from source code.

You can also walk around the C:\j2sdk1.4.2_04\demo\applet directory. You can launch the .html files, which include the appropriate links to load the applets in a browser window. You can also look at the source code for these applets, in the .java files.

- Warren
 
  • #3
Now, how about compiling a program? I'm going to describe the old-school method, which uses the command line. Some people use JBuilder or Forte or other front-end development evironments, and you can also, if you'd like. I think it's useful to know what's going on at the bottom.

Create a new folder for your new project somewhere. Let's say I make a folder called C:\class.

Inside this folder, I'm first going to put a convenience file: make.bat, which will compile my code.

make.bat might look like this:
Code:
path=c:\j2sdk1.4.2_04\bin
javac *.java
and it simply runs the javac program on all files ending with .java in the current directory.

Now, we need to write a bit of Java code. Use this bit, and put it in a file called Test.java:
Code:
public class Test {
	public static void main(String[] argv) {
		System.out.println("Hello world!");
	}
}
Don't worry about what it does just yet -- we just want to get the compiler fired up to make sure it works.

Now, open up a command line (Start->Run->"command") and get into your class directory (type "cd c:\class").

Finally, type "make" and hit enter, and you should see it run. You should now have a Test.class file in the your directory. You've compiled your first Java program!

- Warren
 
  • #4
Now that you've got a Test.class file, you're all ready to run it.

First, I'll make another convenience file, run.bat, which simply frees me from having to type the full path of the java.exe program.
Code:
path=c:\j2sdk1.4.2_04\bin
java Test
When you type "run" on the command line, you're just running the java.exe program and telling it to run the program called Test, which is inside the Test.class file.

If it all goes well, you'll see "Hello world!" printed to your screen.

Let me know if you're following up to here -- you've now gotten past all the difficult first steps in getting your development environment set up, and you're ready to begin learning the Java language.

- Warren
 
  • #5
turin said:
1. Just as a naive question, can we define "program." I might get confused what we're talking about. Apparently (thanks to your concise delineation), what I used to call a program should actually be called "source code." Then, there's object code (which I don't quite understand). So what is a "program?" Is this just a synonym for "source code?"
The word "program" is used rather arbitrarily. Usually it does indeed mean "source code," but it can also mean "executable file." An executable file, is, of course, the result of compiling source code. Since there's a one-to-one correspondence between source code and the resulting object code, you can use the term "program" interchangably.
2. What's a "smart?"
Intelligence. What I meant by executed directly by a physical processor, which doesn't have too many smarts is simply that physical processors are designed to run only very simple steps, like "add this memory block to this memory block," not complex ones like "figure out what this programmer meant." The compiler's job is to figure out what the programmer means and generate code that can be run directly on the dumb processor.
3. Is "object code" the same thing as "machine code" or "machine language?" It seems like this would actually be a lot simpler than learning a foreign language. Well, I'm not here to argue, though :smile:
Object code and machine code are interchangable. Machine language, on the other hand, may refer to assembly language which is basically a mnemonic form of raw machine code that makes it a bit easier to read. Generally, I'd stick with object code or machine code.
4. I don't get this runtime environment thing. Is this just something like iexplorer? I mean, I thought that the whole point of compiling was to make an exec that could be run on any computer. Can you elaborate a little bit more on this. Is java special out of all other languages in this respect?
Java is special, because it has a purpose of being able to run on any computer, with any physical processor. You can compile your Java code on Windows and run it on Linux, for example. The way it accomplishes this is to compile to a platform-independent bytecode, which does not directly run on any physical processor. Bytecode is essentially machine code for a processor that doesn't really exist. Then a program called a virtual machine emulates that idealized Java processor on top of a real processor. Each platform (Windows, Linux, Solaris, etc.) has a Java virtual machine written for it. It effectively translates the bytecodes into real physical instructions the processor can run.
5. Do you think you could walk me through how to install these programs on the office computer. I may not have permissions (I think that's what it's called when the computer is set up to allow installations). I know it is running Win2000 service pack 4 from looking at start > control panel > system (of course, none of that really means anything to me, because, like I said, I'm computer stupid). I can pull up the Add/Remove programs dialogue. Well, I haven't tried anything yet, so this is just a heads-up that you might be hearing me complain about not being able to install this stuff. I suppose I could bring my laptop up here.
I'm afraid you'll have to defer to your office administrator.
6. What does it mean to "launch" an html file?
To open it with a web browser.
7. What's a convenience file and why is it needed? I thought that the compiler compiled, not some convenience file. This is exactly the kind of stuff that does confuse me. :confused: When you say, "it looks like this:" and there's some text, is this like a DOS command line thing?
The two convenience files (run.bat and make.bat) are only shortcuts. You could just type "c:\j2sdk1.4.2_01\bin\javac.exe *.java" everytime you want to compile your code, but you'd get tired of that. Instead, I just put those statements inside a file, called run.bat. Then, you can just type "run."

- Warren
 
  • #6
Make sure that the "Test.class" file ends up in the same directory as your run.bat file. Test.class is created upon compilation in the same place as Test.java.

Forgive me for not mentioning the case sensitivity!

- Warren
 
  • #7
What do you get by simply typing "java Test" on the command line?

- Warren
 
  • #8
The line 'java Test' invokes java.exe, the Java virtual machine, and runs your Java program, stored in Test.class. If you got a Test.class file, it means compilation succeeded. If you're getting a "ClassDefNotFoundError," it means java.exe is also running properly. I'm not frankly sure why it's not finding Test.class on your machine. What version of Windows are you running?

You can also try running the command "java -cp . Test" (note the spaces). The dot by itself means "this directory," and "-cp ." means "classpath here in this directory." The classpath is the list of places where the JVM will look for classes. If you run it with "-cp ." you will force the Java virtual machine to look in that directory for its files.

I'll also attach a zip file with my files, which are apparently exactly like yours.

- Warren
 

Attachments

  • class.zip
    854 bytes · Views: 228
  • #9
chroot said:
What version of Windows are you running?
98.




chroot said:
You can also try running the command "java -cp . Test" (note the spaces). The dot by itself means "this directory," and "-cp ." means "classpath here in this directory." The classpath is the list of places where the JVM will look for classes. If you run it with "-cp ." you will force the Java virtual machine to look in that directory for its files.
Isn't this the whole point of:

c:\cd class

at the beginning of the procedure, though? Does java.exe only look at a specific directory if I type:

java Test

on the command line, or does it look all over the computer?
 
Last edited:
  • #10
Well, the trouble you're having executing "java Test" may be a result of your running Windows 98.

Basically, there's an environment variabled called CLASSPATH. This variable contains a list of locations to search for .class files. It may or may not be set properly on your computer. When you just type "java Test," the java.exe program looks at your CLASSPATH variable, which might point to any number of places on your system. There's a good chance the very primitive Microsoft Java stuff that came with Windows 98 is setting up its own CLASSPATH.

So, to override it, just use the -cp switch on java.exe. In other words, type "java -cp . Test" and it'll force java.exe to look only in the current directory for class files. That should solve the problem. Lemme know what happens.

edit: I mean, edit the run.bat file so its second line is "java -cp . Test"

- Warren
 
  • #11
I'm having trouble getting the applet to show. This is my code:

import java.applet.Applet;
import java.awt.*;

public class AppletTime extends java.lang.Object {


public AppletTime() {
}

public class Picture extends java.applet.Applet{
public void paint(Graphics g){
for (int x=0;x<251;x++){
g.drawLine(0, x, 500, x);
g.setColor(new Color(255,0,x));
}


}
}

public static void main(String[] args) {
}

}



I don't know what to do?
I think it has to show in Netscape Explorer, and I read something about making the code a superclass so it can run in Netscape Explorer but I'm not sure how to do it. Any suggestions?
 
  • #12
First, your applet needs to be a subclass of Applet, not Object.

Second, you need to write an html document to include the Applet. You then open the html file with IE or Netscape or whatever.

Please see the examples in the demo folder. If you're using J2SDK 1.4.2, they'll be in c:\j2sdk1.4.2_04\demos.

- Warren
 
  • #13
empire:
you could use the appletviewer utility which comes with the JDK to test your applet without writing an html document. of course if you use html not only you get to learn something new, but you can be sure the applet behaves as you intend.
Also as I know, a applet does not have a main() function, but instead has init(), start() and paint() ... what you have here appears an attempt to start an applet from command line (?)
 
  • #14
Cool! Now, if someone can help exaplain stacks and queues, I'd be straight.
 
  • #15
stack iz a data structure which follows last-in and first-out rule. that means the most recent data stored in the stack will be the first one to go out...
the queues follow the first-in and first-out rule instead.
 
  • #16
Using threads, how do I make a string blink?
 
  • #17
I think you might find an example in the JDK folder ... there are a couple of applets there.
But a quick thought is to make a while loop in the run() function in which you wait for t1 milisecs, draw the string, wait for t2 milisecs, erase the visible area .
 
  • #18
Hmmm sorry to have neglected this thread. I need to consider what sort of code to post next... Any suggestions? Turin?

- Warren
 
  • #19
maybe some basic file operations... :)
 
  • #20
chroot,
Apparently we are all going to use this thread as a message board, so I will make this my new default and leave all of my posts permanent.




chroot said:
Hmmm sorry to have neglected this thread.
I just appreciate the fabulous job you did to get me started. I have been lately worried about finding a job and so have also neglected this java pursuit.




chroot said:
Hmmm sorry to have neglected this thread. I need to consider what sort of code to post next... Any suggestions? Turin?
My initial/immediate goal is to be able to annimate ... I suppose that entails using java to display a series of gifs in a web browser one after another at a certain rate. I would like to learn how to do that for starters, so maybe you could organize such a program. I looked at (what I think is) the source code in the demo folder of that package I downloaded, but I could make just about 0 sense out of it.

I got the "hello world" to display, but all I learned from that is how to make the DOS shell display text, which I could do much more immediately before w/o the java.

I have absolutely no interest at all in annoying blinking text or anything related to web advertisement. Am I barking up the wrong programming language?
 
  • #21
I think you might find an example in the JDK folder

Where is the JDK folder?
 
  • #22
Hey turin,

Okay, let's begin with the simplest possible GUI first, to get you acclimated. I'll post the same code first without comments (so you can see the entire structure more clearly), and then again with detailed comments.

Code:
import java.awt.*;
import javax.swing.*;

public class Test {
	protected JFrame		frame;

	public Test() {
		frame = new JFrame("My First Window");

		frame.getContentPane().add(new JLabel("Hello world!"));

		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] argv) {
		new Test();
	}
}

Code:
// The following two lines allow us to use classes like
// javax.swing.JFrame without having to explicitly
// include "javax.swing." every time.
import java.awt.*;
import javax.swing.*;

// Everything in Java goes in a class of some kind -- we
// just called this class Test.
public class Test {
	// This is a variable which belongs to the Test class.
	// Remember the definition of a class: it's a collection
	// of both data, and the operations that work with that
	// data. You can think of this variable as being "global"
	// within the class, but not outside.
	protected JFrame		frame;

	// This method, which has the same name as the class, is
	// special. It's called a 'constructor,' and is the method
	// that is called automatically when a Test object is
	// created. The constructor's job, in every class, is
	// to initialize the class's variables into a usable state.
	public Test() {
		// This line, with the "new" keyword, creates a new object.
		// In this case, we're creating a new JFrame object, which
		// is a window on the screen. We're passing one argument
		// to the "new" operator, a string. That string is directly
		// passed to the JFrame's constructor. The JFrame uses the
		// string for the window's title.
		frame = new JFrame("My First Window");

		// Here we're wrapping two steps into one line. First,
		// we're creaing a new JLabel object, with the text
		// "Hello World" in it. Second, we're passing that
		// JLabel object to the add method of the frame.
		// In other words, we're creating a label and adding
		// it to the window in one line.
		frame.getContentPane().add(new JLabel("Hello world!"));

		// JFrames start life hidden; you have to explicitly
		// make them visible after creating them. We're going to
		// first call the "pack" method, which packs all the
		// JFrame's components into their best positions in the
		// window, and then call "setVisible," with an argument
		// of true, to show the window.
		frame.pack();
		frame.setVisible(true);
	}

	// This is the "main" function -- every program must have one,
	// but you don't need to have one in every class; just the
	// "toplevel" class. Since this program has only one class,
	// it obviously must go here. For now, ignore the static keyword.
	// This is a function that is accessible to the world (it's
	// public), returns nothing (it's void), and accepts an array
	// of Strings as an argument. The strings are the options typed
	// by a user on a command line. If you typed 'java Test apple',
	// for example, 'apple' would be in this array.
	public static void main(String[] argv) {
		// All we're going to do is create a Test object.
		new Test();
	}
}

Any questions?

- Warren
 
  • #23
chroot said:
Any questions?
:redface:
Uh, ya. Now I will probably really start to frustrate you.

Let's pretend that I don't speak programmer.

I am unclear about the meaning of the following terms:
- class
- variable
- method
- create
- object
- state
- component

I am unclear about the significance of the following issues:
- the first two lines: what they are and why you have them
- what it means to "use a class"
- how the Jframe object has been identified as "a window on the screen"
- what it means to be "in a class"

Affirmations:
- the amount of white space is inconsequential?
- the symbol "//" tells the compiler to ignore everything to the right?
 
  • #24
Okay, let's start with the top, then. I thought you were perhaps already familiar with object oriented programming.

Basically, a program is broken up into components called objects. An object can (and often does) represent a physical object. For example, an object can represent a disk, or a file, or a printer.

An object contains two things: data, and methods. Data store the state of the object, while methods are pieces of code that operate on the data. For example, a printer object would store information like the printer's model number, whether or not it is out of paper, how much ink is left, and so on. The printer object would also have methods that can be called by a programmer. A simple method might be "print()", to which a programmer passes a page object to be printed. Another might be "getStatus()," so that a programmer can tell if the printer is busy or not.

How about another example? A chess game might include objects for pawns, rooks, bishops, and so on. Each object contains some state information (like the piece's position on the board) and some methods (like a move() method that tells the piece to move somewhere else).

So that's it: objects contain data and methods. The data encode the state, the methods operate on the state.

A class is like a blueprint for an object. A class describes the slots into which an object stores its data (they're called variables), and the actual code that makes up its methods.

For example, here's a simple ChessPiece class:

Code:
publc class ChessPiece {
	public int	x;
	public int	y;
	
	public void move(int newx, int newy) {
		x = newx;
		y = newy;
	}
}

The reason a class is a blueprint for an object is simple: I can make many ChessPieces. I can have 16 on a board, each one created from this blueprint. They would all be identical at the start. Each ChessPiece maintains its own independent x and y variables, and I have to specify which piece I mean when I use the move() method.

When you use the new operator to make a new object ("new ChessPiece(), for example, makes a new ChessPiece object), you're actually automatically allocating the space in memory for the new piece's x and y variables. You can say that you are creating a ChessPiece object from the blueprint.

Let's see about the other questions:

The import statements are just a convenience. The full, complete name of the JFrame class is actually javax.swing.JFrame, where javax.swing is a package of interrelated classes. For convenience, importing all of the javax.swing class (using the * for "all") allows us to use JFrame without having to specify its entire pedigree.

The JFrame class was written by some folks at Sun to work with your operating system to display a window. Hence, the JFrame class represents a window.

The variables and methods belonging to a class (like x, y, and move in the ChessPiece) can be said to be "in the ChessPiece class."

And yes, whitespace is ignored -- it's just for human readability. Anything to the right of // is considered a comment, and is also ignored by the compiler.

- Warren
 
  • #25
chroot,
Do you teach a programming language? You would be an EXCELLENT instructor! I have spent weeks trying to read through Visual C, C++ books, but they never made sense to me. What you have said is - beautiful. OK, enough smoke up the ass. I'll have to dedicate the evening to absorbing all of this and get back to you tomorrow.
 
  • #26
turin,

I have indeed taught programming classes in the past; I'm glad you find my writing so helpful, even when it's not all that well-organized and somewhat rushed!

- Warren
 
  • #27
chroot, just one question if i may. My current connection is too slow to download that, but MS visual studio says that it contains "visual j++", do you know anything about that? Thanks.

Is that something I'd have to get individually and is not contained in VS by default? Because I can't find a way to use it, but the MSDN has loads of documentation on it..
 
Last edited:
  • #28
I believe you can compile and run most Java programs with Visual J++. J++ was Microsoft's attempt to extend and proprietize Java so that it would get locked to the Windows platform -- it was thankfully not very successful.

The only downside is that you won't be running the latest and greatest Java release, so you might have to deal with bugs or deprecated classes. Most of the code we'll write here, however, is simple enough that it should run without problems for you.

- Warren
 
  • #29
Thanks, I'll try to get it to run.. Or try to get someone to download the java SDK for me.. If I learn something, I may as well learn it the right way.. :)
 
  • #30
chroot said:
... it's not all that well-organized ...
I would say that it's well targetted. :wink:


chroot said:
... it's ... somewhat rushed!
Brief and to the point, I think you mean. :wink:




I tried to invoke the make.bat file, but I got errors. I have attached a zip file that contains all contents of the c:\class folder.

I put the most recent code (the stuff that we're currently discussing) into the lesson2.java file. Then I double clicked on the make.bat file and the command window came up. After a few seconds, some lines appeared that said some errors. I have included everything from this command window output (as verbatim as possible) in the lesson2_errors.txt file.



Here's what I think I understand:

- 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.

- There are some combinations of letters that the compiler recognizes for a specific purpose. The JFrame in the source code is one such example.

- 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.

- 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?



Here's what I still need clarified:

- 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. I can see how source code written in one of these languages will work by following it through its algorithm. In this java code, I don't see any commands or algorithmic structure; it just looks like a bunch of random lines of code. Is this just because it is such a simple program? Even when I looked at the source code for some of the demos it seemed like the same situation. It seems that the java source code is almost like lecture notes whereas the programming I'm used to is more like instructions for putting together a model airplane. Don't computers need instructions? Do I need to start thinking differently about programming? Is this what you meant by object oriented programming?

- 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?

- 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?

- 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?

- 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?

- 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.



Now that I have gone through all of these questions, I think I am actually starting to see the resolutions to these issues. I would still appreciate your comments, though.
 

Attachments

  • lesson2.zip
    1.6 KB · Views: 363
Last edited:
  • #31
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
 
  • #32
chroot said:
turin,

I have indeed taught programming classes in the past; I'm glad you find my writing so helpful, even when it's not all that well-organized and somewhat rushed!

Perhaps, you should think about writing a book. I'd buy it.
 
  • #33
chroot,
I think I'm starting to get it. It is so hard in the beginning (I hope just in the beginning).

So, when one writes a *.java file, the purpose is to create a class? Then after writing 18 thousand (give or take) *.java files, one writes an "umbrella" or "master" (or "main"?) *.java file that takes advantage of the fact that most of the code work has been taken care of by the "sub" files by importing them and simply "calling" or "instantiating" (or "creating"?) them?

When you say that JFrame has its own file called JFrame.java, do you mean JFrame.class? It seems like the extension "java" indicates waiting to be complied (or source code) and the extension "class" indicates compiled code (or object code).

I'll probably try to play with lesson2 tonight (Fri night, I'm such a nerd).
 
  • #34
Yes, you've basically got it... you write a bunch of classes, stick each in its own .java file, and compile them. Then you have one top-level class, which contains a special method called "main," that is the entry point to your entire program. The main method would then go and create whatever objects are used in the program, and so on.

And there was once a JFrame.java file somewhere, that someone wrote. If your JDK distribution includes the java source files, then you actually have a copy of it. If your JDK distribution did not come with the source, then you only have the JFrame.class file, which was compiled from the JFrame.java source code. Keep in mind that Java uses .jar files, basically the same as .zip files, to store large numbers of classes in an efficient way. There is probably a "rt.jar" file somewhere in your JDK directory. It stands for "runtime," and contains all of the classes like JFrame.class. Feel free to open the rt.jar file up with WinZip and look at what's inside.

- Warren
 
  • #35
One thing that I forgot to ask about:

The main program imports all of the other subprograms that are written, but, for instance, JFrame.class is not in the c:\class directory. I'm assuming that I need to put it there? In general, do I need to put all of the supporting *.class files together or specify a path in the main program? How does that part work?
 

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
33
Views
8K
  • Quantum Physics
Replies
9
Views
1K
  • STEM Academic Advising
Replies
5
Views
1K
  • STEM Academic Advising
Replies
13
Views
2K
Back
Top