Java Coffee Break Newsletter Volume 2, Issue 2 http://www.davidreilly.com/jcb/ ISSN 1442-3790 ========================================================================= In this issue * In the news * Tutorial : Introduction to JBuilder * Q&A : How do I create a new instance of an object? * Q&A : I'm confused about variables and their scope. What's the difference between a member variable, and a local variable? ========================================================================= In the News Here are a selection of recent news items that may be of interest to Java developers. /*/ The Jini is out of the bottle Sun's Jini technology promises to allow electronic devices to easily connect together. Imagine a wristwatch running a JVM, and controlling other devices such as your stereo or alarm system. Sound like fiction? Visit Sun's JINI site for info : http://www.sun.com/jini/ /*/ Microsoft complies with prelimary ruling by releasing service pack Microsoft is offering a service pack, which allows developers to turn off certain features of the Visual J++ package which render applications unable to run on a non-Windows platform. For more information see the Visual J++ site, at http://www.microsoft.com/visualj/ /*/ Microsoft's enthusiasm for Java is cooling Microsoft is considering alternatives to Java, and is rumoured to be working on a new language, code-named "Cool". The question is, just how loyal are Java developers? See PC Week's article on the subject, at http://www.zdnet.com/pcweek/stories/columns/0,4351,389401,00.html ========================================================================= Tutorial Introduction to JBuilder Borland JBuilder is a popular package for Java programmers, and is used by many educational institutions for Java courses. This tutorial gives a brief introduction to the tool. The HTML version offers easy-to-follow diagrams, and can be found at http://www.davidreilly.com/jcb/jbuilder/lesson01/ Overview The goal of this lesson is help you to become familiar with the Borland JBuilder tool, and for you to write, compile, and execute a simple Java application that displays the message "Hello world!". Step One - Creating a project Start by loading the Borland JBuilder tool. JBuilder remembers the most recent project you've worked on, and automatically opens it for you. Since we're creating a new project, however, we need to close it and create a new project. * From the 'File' menu, select the 'Close All' menu option. * From the 'File' menu, select the 'New Project' menu option JBuilder will now display a Project Wizard. This wizard will ask you for information about your project, and then generate a project file for you. * Enter as the file name for your project "MyFirstProject.jpr". It's best to place projects in their own directories. You can also click the browse button to use a file selection dialog box. * Enter as a title 'My First Project', your name, and a brief description. * Click 'finish'. Step Two - Creating a source file Once you've created a project, you can begin to write your first Java application. The project view allows you to manage your project's files. You can select a file, and edit it, or you can add/remove files using the 'Add to project' and 'Remove from project' buttons. These can be easily recognised by the plus and minus signs on these buttons. To write an application, we'll need to create a new source file, which we'll call "HelloWorld.java".  * Click on the 'Add to project button' (the blue folder with a green plus icon). * As a filename, use 'HelloWorld.java'. * Click on the 'Open' button to create the file. Step Three - Writing your first application Up until now, we haven't had to write any code. Now its time to get your hands dirty. Start by typing the following code into the "HelloWorld.java" file from Step Two. package MyFirstProject; public class HelloWorld { public static void main(String args[]) throws Exception { System.out.println ("Hello world!"); System.in.read(); } } Don't worry if you're a little unsure about what the code does. We'll examine the code line by line. 1: package MyFirstProject; This line states that the class belongs to the package MyFirstProject. A package is a collection of Java classes and interfaces. We group logically related code together in a package. As a general rule, each application should have its own unique package. Use the same name for your package as you do for your project. 2: public class HelloWorld This line declares a new class, called "HelloWorld", and that it is publicly accessible. A class is composed of data variables (members), and functions (methods). 3: public static void main(String args[]) throws IOException All applications written in Java share at least one thing in common - they all have a main method. You must declare a public static main method that accepts command line parameters (args), for your application to run. Our main method also declares that it could generate an I/O error when reading input from the user. 4: System.out.println ("Hello world!"); This is where our application writes a message to the user. System.out is an object that allows us to write to "standard output", which is the user's console. 5: System.in.read(); So that you can read the message before the application terminates, we need to wait for keyboard input. System.in is an object that allows us to read from "standard input", which is the sequence of characters entered by the user. Step Four - Compiling the application Before you can run an application, you must first compile it. A compiler takes source code (the instructions for a program), and converts it into executable instructions (a program that can be run). We'll compile the HelloWorld application now. * From the 'Build' menu, select the 'Make "HelloWorld.java" menu item. If you've typed in the code correctly, your application will now be ready to run. If not, carefully check your code again, and repeat. Step Five - Running the application Once the application is compiled, its ready to run. JBuilder runs applications in their own window, which you'll see in a moment. * From the 'Run' menu, select the 'Run "HelloWorld" menu item, or use the shortcut of shift-F9 The HelloWorld application will then appear in a new window. You'll see the message "Hello World!", and the program will wait patiently until you press the key. Summary At the conclusion of this lesson, you should be able to do the following :- * Create a new project * Add files to your project * Write code to send messages to standard output * Compile code * Execute compiled code For the full article, including graphics diagrams, see http://www.davidreilly.com/jcb/jbuilder/lesson01/ ========================================================================= Q&A : How do I create a new instance of an object? To create a new instance of an object, we use the "new" keyword. This keyword creates a new instance of an object, which we can then assign to a variable, or invoke methods. For example, to create a new StringBuffer object, we would use the new keyword in the following way. StringBuffer  myBuffer  = new StringBuffer (50); Notice how we can also assign parameters (in this case, we are allocating at least fifty characters for our buffer). If we don't wish to specify any parameters, we could use the new keyword this way. StringBuffer  myBuffer  = new StringBuffer (); The new keyword expects a class name, followed by the parameters that will be passed to a constructor. When we have no parameters, we simple use empty ()'s. ========================================================================= Q&A : I'm confused about variables and their scope. What's the difference between a member variable, and a local variable? Simple! A member variable is a variable that belongs to an object, whereas a local variable belongs to the scope of a function. Hmm.. Still confused? When we define a class, we can give that class member variables. These variables are members of that class. Take, for example, the following class declaration. public class MyClass { int a; int b; } MyClass has two member variables, a & b. When we define an object instance of MyClass it will still have two member variables, a & b. We can reference these members using the '.' character. // Create an instance of MyClass MyClass obj = new MyClass(); // Assign new values to a & b obj.a = 1; obj.b = 65536; These variables belong to MyClass, and are known as member variables. On the other hand, if we declare variables inside of a function, we call these local variables. These variables are local to a particular function, and not publically accessible. For example, in the following function, we have no way of accessing the obj variable outside of the main(String[]) function. public static void main (String args[]) { MyClass obj = new MyClass(); System.out.println ("A" + obj.a); } ========================================================================= The Java Coffee Break Newsletter is only sent out to email subscribers who have requested it, and to readers of the comp.lang.java.programmer and comp.lang.java.help newsgroups. If you'd like to receive our newsletter, and get the latest Java news, tips and articles from our site, then get your FREE subscription from http://www.davidreilly.com/jcb/newsletter/ If you are an email subscriber and no longer wish to receive the JCB Newsletter, please unsubscribe using the WWW form located at http://www.davidreilly.com/jcb/newsletter/unsubscribe.html