Tuesday, February 3, 2009

Did you know: Static import

Static Import is a very simply and unnecessary opportunity in Java.

Static Import

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;
or:

import static java.lang.Math.*;

Once the static members have been imported, they may be used without qualification:

double r = cos(PI * theta);

It's easy and unnecessary as I said.

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. }

Wednesday, December 31, 2008

Some interesting facts from year 2008

You, visitors
I have had visitors from 73 country. This countries from Google Analytics:


  • Hungary
  • United States
  • India
  • United Kingdom
  • France
  • Germany
  • Canada
  • Australia
  • Italy
  • Netherlands
  • Poland
  • Singapore
  • Sweden
  • Russia
  • Philippines
  • Venezuela
  • Israel
  • Turkey
  • Switzerland
  • Spain
  • Mexico
  • Norway
  • Indonesia
  • Ireland
  • Belgium
  • Portugal
  • Denmark
  • Honduras
  • Brazil
  • Greece
  • Thailand
  • South Korea
  • South Africa
  • Argentina
  • Czech Republic
  • Finland
  • Ukraine
  • Slovakia
  • Egypt
  • Austria
  • China
  • Hong Kong
  • Malaysia
  • Sri Lanka
  • Iran
  • Bulgaria
  • Vietnam
  • Pakistan
  • Romania
  • Chile
  • Jordan
  • Slovenia
  • New Zealand
  • Belarus
  • Serbia
  • United Arab Emirates
  • Saudi Arabia
  • Kenya
  • Iceland
  • Malta
  • Peru
  • Latvia
  • Luxembourg
  • Kuwait
  • Syria
  • Lebanon
  • Mongolia
  • Cyprus
  • Colombia
  • Croatia
  • Japan
On the map:

Search Engines
I searched the "Java Blog" (include quote marks) expression in some search engine, the result was:
  • Google: I was the 41, Google PageRank give me 1/10.
  • MSN: I tried, but didn't find myself in first 100.
  • Yahoo: I tried, but didn't find myself in first 100.

Keyword java give 34,600,000 results in Google, 258,000,000 in MSN, 70,000,000 in Yahoo.

Posts
20 posts have been written, this is the 21th. First post is written at June 21, 2008.

Comments
I have had only one comment from you, I hope number of comments will be increased.

Friday, November 28, 2008

Show Image In JTable

In this short post I present, how can we put and show images in JTable component.

Problem
Sometimes we need to put and show images in JTable. A typical situation is when we have a boolean true or false and we don't want to write true/false or yes/no captions.

Solution
The solution is very simply. We have to override the
getColumnClass(int columnIndex)

method of TableModel class, like this:

DefaultTableModel model = new DefaultTableModel() {
@Override
public Class getColumnClass(int columnIndex) {
//Set the index of the column, where the images will be.
final int imageIndex = 5;
return columnIndex == imageIndex ? ImageIcon.class : Object.class;
}
};

I hope it will be useful.

Tuesday, November 25, 2008

Solve IllegalStateException: Illegal to call this method from injected, managed EntityManager

Problem
If we injected an EntityManager into our SessionBean and call the entityManager.getTransaction().begin() method then
IllegalStateException: Illegal to call this method from injected, managed EntityManager
exception is threw.

Java Enterprise version: JEE5
Used server: JBOSS 5.0.0.RC1
EJB version: EJB3


Why did this problem occur?
We wanted to use User-managed transaction, but this is not supported this way.

Solution
To solve this problem, we should use UserTransaction. To do this, inject a javax.ejb.SessionContext instance as Resource:
@Resource
SessionContext sc;


After this we can get an UserTransaction instrance in our method this way:
public void someMethod() {
UserTransaction ut = scgetUserTransaction();
ut.begin();
//We have an active transaction, we can access database use EntityManager intance.
ut.commit();
}

I hope it will be useful.

Saturday, October 25, 2008

Big Java Blog is continued

Dear Visitors,
unfortunately I did not have time and energy to write posts in the last weeks. Now I make a promise, I return to blogging.

The puzzles, the SCJP Exams and Spring themes will be continued and I try present much more interesting Java technologies.

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.