Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Class Declaration

To declare a class in JavaFX, use the class keyword.

public class Title {

The public keyword is called an access modifier and means that this class can be used by any other class or script, even if that class is declared in another script file. If the class does not have a modifier, it is only accessible within the script file where it is declared. For example, the class Point in Listing 3.2 does not have a visibility modifier, so it is only has script visibility and can only be used within the ArtWork script.

Listing 3.2. Artwork.fx

class Point {// private class only
             //visible to the ArtWork class
    var x:Number;
    var y:Number;

public class ArtWork {
    var location: Point;

To extend a class, use the extends keyword followed by the more generalized class name. JavaFX classes can extend at most one Java or JavaFX class. If you extend a Java class, that class must have a default (no-args) constructor.

public class Porsche911 extends Porsche {

JavaFX may extend multiple JavaFX mixin classes or Java interfaces. Mixin classes are discussed in the next section.

An application may contain many classes, so it is helpful to organize them in a coherent way called packages. To declare that your class or script should belong to a package, include a package declaration at the beginning of the script file. The following example means that the Title class belongs to the com.mycompany.components package. The full name of the Title class is now com.mycompany.components.Title. Whenever the Title class is referenced, it must be resolved to this full name.

package com.mycompany.components;
public class Title {

To make this resolution easier, you can include an import statement at the top of your source file. For example:

import com.mycompany.components.Title;

var productTitle = Title{};

Now, wherever Title is referenced within that script file, it will resolve to com.mycompany.components.Title. You can also use a wildcard import declaration:

import com.mycompany.components.*;

With the wildcard form of import, whenever you refer to any class in the com.mycompany.components package, it will resolve to its full name. The following code example shows how the class names are resolved, showing the fully qualified class name in comments.

package com.mycompany.myapplication;
import com.mycompany.components.Title;

// com.mycompany.myapplication.MyClass
public class MyClass {
     // com.mycompany.components.Title
     public var title: Title;

A class can have package visibility by using the package keyword instead of public. This means the class can only be accessed from classes within the same package.

package class MyPackageClass {

A class may also be declared abstract, meaning that this class cannot be instantiated directly, but can only be instantiated using one of its subclasses. Abstract classes are not intended to stand on their own, but encapsulate a portion of shared state and functions that several classes may use. Only a subclass of an abstract class can be instantiated, and typically the subclass has to fill in those unique states or behavior not addressed in the abstract class.

public abstract class MyAbstractClass {

If a class declares an abstract function, it must be declared abstract.

public abstract class AnotherAbstractClass {
    public abstract function
                  setXY(x:Number, y:Number) : Void;
  • + Share This
  • 🔖 Save To Your Account