Showing posts with label trick. Show all posts
Showing posts with label trick. Show all posts

Monday, February 2, 2009

Protected Access Modifier Facts

I show an interesting example to the protected access modifier.



Our first file, Animal.java in package world:
01. package world;
02. public class Animal {
03. protected int age;
04. }


Our second file, Animal.java in package africa:
01. package africa;
02. import world.Animal;
03. public class Zebra extends Animal {
04. public void sayAge() {
05. System.out.println("My age is "+age); //Works well, age is protected;
06. }
07. public void sayFriendAge() {
08. Animal friend = new Animal();
09. System.out.println("My fried's age is " + friend.age ); //Compile error, age is accessible
10. // through inheritance
11. }
12. }


Our third file, Lion.java in package africa:
01. package africa;
02. public class Lion {
03. public void sayZebraAge() {
04. Zebra zebra = new Zebra();
05. System.out.println("Zebra's age is " + zebra.age ) ; //Compile error, Zebra and Lion are
06. // in the same package, but age is protected and only available using inheritance, Lion
07. // can't see it.
08. }
09. }

Friday, August 29, 2008

Load jar files and Java classes dynamically

In this post I present a way to load jar files and Java classes dynamically from java code. It can be very useful, if you want to create module, for example.

Design Pattern
The solution build up from three part, the
Loader - loads and uses the jar files and Java class dynamically,
Communication interface(s) - defines the interface, what can be used to communicate the program and the dynamic library,
Dynamic libraries - the Loader loads and uses this libraries through the communication interface(s).

The loader, the communication interface(s) and the dynamic libraries must be separated in different projects, because
  • Loader imports the communication interface(s)
  • Dynamic libraries imports the communication interface(s)
  • Loader can't know anything about the Dynamic libraries
  • Dynamic libraries can't know anything about the Loader



Sample application
As usually, I made a sample application, what can help to you to understand my idea. The sample application is very similar now. The main program can be invocated with two parameter, the first is the name of the jar file to load, the second is the name of the class to load. The application loads the given jar file and class and say hello in two different language, depend on, which jar file is loaded.

Creating the communication interface(s)
There is only one communication interface, what defines only one simple method:



Creating a dynamic library
The sample dynamic library is as simple as the communication interface:


Creating the Loader
Sample application has a main class, what processes the program arguments:


and a class, where the codes are separated, that load the jar file and class dynamically:


Downloads
You can download sample projects here as NetBeans project.

Friday, June 27, 2008

Pop-up Window with Return Value

Trick
In this post a trick is presented: possibility to return object from a pop-up dialog.

It can be useful to
  • select and return an element of list from a pop-up dialog
  • create a new object in a pop-up dialog and return it
  • edit an object in a pop-up dialog and return it
Participants
Caller - the place, where the pop-up window is opened and the returned data is processed.
Dialog - this is the pop-up window, what produces the return object.

Step 1: Creating the dialog
Create a GUI window, what extends the javax.swing.JDialog class, like this:



My sample dialog creates and returns a String instance from a TextField.

Step 2: Extend the dialog code
First add a new class member to the created dialog class:

private <ReturnObjectClass> returnValue;

where the <ReturnObjectClass> is the class of the object, what our pop-up window returns, in my case:

private String returnValue;

Now write the actions to the buttons.
Cancel button close the window and return null:

returnValue =null;
dispose();

Return button close the window and return the new String:

returnValue = jTextField1.getText();
dispose();

At least construct the method, what return the created object:

public String getValue() {
this.setVisible(true);
return returnValue;
}

Step 3: Creating the caller
Create a class, named Main, and put the following code:

public static void main(String[] args) {
String s = new GetStringDialog(null, true).getValue();
System.out.println("Dialog returned with: " + s);
}

After you run this program, the pop-up dialog is showed and after clicking the Return button the typed string is written to the console.

Downloads
You can download sample code here as NetBeans project.