Sun Java Solaris Communities My SDN Account Join SDN

Learning the JavaFX Script Programming Language - Tutorial Overview

Lesson 11: Access Modifiers

   
« Previous 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »
 
Now that you understand packages, we can discuss the various access modifiers provided by the JavaFX Script programming language. These special keywords allow you to set various levels of visibility for your variables, functions, and classes. For more information, see Chapter 8. Access Modifiers in the JavaFX Script Programming Language Reference.
 
Contents
 
Default Access
The package Access Modifier
The protected Access Modifier
The public Access Modifier
The public-read Access Modifier
The public-init Access Modifier
 
Default Access

The default access, known as "script-only", is what you get when no access modifier is provided. This is what we've been using throughout most of the tutorial.

Some examples are:

var x;
var x : String;
var x = z + 22;
var x = bind f(q);
 

With this level of access, variables can be initialized, overridden, read, assigned, or bound from within the script only. No other source files can read or access this information.

The package Access Modifier

To make a variable, function, or class accessible to other code in the same package, use the package access modifier:

package var x;
 

Be careful not to confuse this access modifier with package declarations as described in the previous lesson!

Example:

// Inside file tutorial/one.fx
package tutorial; // places this script in the "tutorial" package
package var message = "Hello from one.fx!"; // this is the "package" access modifier
package function printMessage() {
     println("{message} (in function printMessage)");
}

// Inside file tutorial/two.fx
package tutorial;
println(one.message);
one.printMessage();
 

You can compile and run this example (from the tutorial parent directory) with the following commands.

javafxc tutorial/one.fx tutorial/two.fx
javafx tutorial/two
 

The output is:

Hello from one.fx!
Hello from one.fx! (in function printMessage)
 
The protected Access Modifier

The protected access modifier makes a variable or function accessible to other code in the same package, and to subclasses that are in any package.

Example:

// Inside file tutorial/one.fx
package tutorial;
public class one {
     protected var message = "Hello!";
}

// Inside file two.fx
import tutorial.one;
class two extends one {
     function printMessage() {
          println("Class two says {message}");
     }
};

var t = two{};
t.printMessage();
 

Compile and run this demo:

javafxc tutorial/one.fx two.fx
javafx two
 

The output is:

Class two says Hello!
 

Note: this access modifier cannot be applied to classes, which is why we have marked class one as public.

The public Access Modifier

A public class, variable, or function has the most visibility. It can be accessed from any class or script, in any package.

Example:

// Inside file tutorial/one.fx
package tutorial;
public def someMessage = "This is a public script variable, in one.fx";
public class one {
     public var message = "Hello from class one!";
     public function printMessage() {
          println("{message} (in function printMessage)");
     }
}

// Inside file two.fx
import tutorial.one;
println(one.someMessage);
var o = one{};
println(o.message);
o.printMessage();
 

Compile and run this example:

javafxc tutorial/one.fx two.fx
javafx two
 

Output:

This is a public script variable, in one.fx
Hello from class one!
Hello from class one! (in function printMessage)
 
The public-read Access Modifier

The public-read access modifier defines a variable that is publicly readable, but (by default) is writeable only from within the current script. To widen its write access, prepend either the package or protected modifier (i.e. package public-read or protected public-read). Doing so will set its write access to the package or protected level.

Example:

// Inside file tutorial/one.fx
package tutorial;
public-read var x = 1;

// Inside tutorial/two.fx
package tutorial;
println(one.x);
 

Compile and run this example:

javafxc tutorial/one.fx tutorial/two.fx
javafx tutorial/two
 

The output is "1", which proves that x can be read from outside the tutorial/one.fx script.

Now, let's attempt to modify its value:

// Inside tutorial/two.fx
package tutorial;
one.x = 2; 
println(one.x);
 

The result is a compile-time error:

tutorial/two.fx:3: x has script only (default) write access in tutorial.one
one.x = 2;
   ^
1 error
 

For this to work, we must widen the write access of x:

// Inside file tutorial/one.fx
package tutorial;
package public-read var x = 1;

// Inside tutorial/two.fx
package tutorial;
one.x = 2;
println(one.x);
 

The example will now compile and print "2" to the screen.

The public-init Access Modifier

The public-init access modifier defines a variable that can be publicly initialized by object literals in any package. Subsequent write access, however, is controlled in the same manner as public-read (the default is write access at the script-level, but prepending package or protected will widen the access accordingly). The value of this variable is always readable from any package.

Example:

// Inside file tutorial/one.fx
package tutorial;
public class one {
     public-init var message;
}

// Inside file two.fx
import tutorial.one;
var o = one {
     message: "Initialized this variable from a different package!"
}
println(o.message);
 

Compile and run the example:

javafxc tutorial/one.fx two.fx
javafx two
 

This prints "Initialized this variable from a different package!", proving that an object literal in a different package can initialize the message variable. However, since subsequent write access is script-only, we cannot change its value:

// Inside file two.fx
import tutorial.one;
var o = one {
     message: "Initialized this variable from a different package!"
}
o.message = "Changing the message..."; // WON'T COMPILE
println(o.message);
 

The compile-time error is:

two.fx:12: message has script only (default) write access in tutorial.one
o.message = "Changing the message..."; // WON'T COMPILE
 ^
1 error
 

This confirms the expected behavior: the variable can be publicly initialized, but subsequent writes are controlled by a different level of access.

 
« Previous 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »
 
 
Rate This Article
 
Comments
Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies - your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.