Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Classes, methods and parameters - an introduction

The subject is a complex one but we'll try to put PressMe into the context of Java programming structure.

 

 

 

Renaming the source file causes confusing, apparently inexplicable errors for beginners.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Continuing to explain the features of the PressMe.java program:

After the import statements which we'll leave for now, we see the line:

public class PressMe extends Applet implements ActionListener
{
  // rest of program goes in here
}

public refers to a Class or other object which can be accessed by outside Classes, in this case if the Class is not public it can't be run.

PressMe is the name of the class and the file the code is stored in MUST be called PressMe.java.

The PressMe Class extends the Applet Class (as do all Java Applets), this means that the methods of Class Applet are available to PressMe as well. One of the methods is init:

  public void init()
 {
     
// add places objects on the Applet surface in default layout
     //(we have no control)

     add(name);
     add(pressMe);
     add(greeting);
     
// Causes button presses to be detected
     pressMe.addActionListener(this);
 }

A method is a body of code defined by a name (init in this case) followed by round brackets (parenthesis) and with the body enclosed by braces.

The ActionListener referred to in the first line is an interface. This is a special kind of Class which, in this case, provides our Applet with the ability to respond to events on the user interface.

Events are actions made by our users - a typical example is clicking a button with the mouse.

Sometimes the parenthesis after the method name hold parameters - references to other objects:

 public void actionPerformed(ActionEvent e)
  {
    // set the label text to this String
    greeting.setText("Hello " + name.getText());
  }

The parameter of the method actionPerformed is (ActionEvent e) - ActionEvent is the object type and e the identifier.

Coming up next, some more Applets.

Related: [ Java Home | previous:syntax | next:Adding Applet ]

Teaching notes

We will have much more on this topic later.

So it could be skimped or even skipped if desired.

A glossary is an excellent idea - if you are using Moodle or a similar tool, consider having the students add the jargon words to it.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009