<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Java Programming Forum - Learn Java Programming - Blogs - Hibernate</title>
		<link>http://www.java-forums.org/blogs/hibernate/</link>
		<description>Java Programming Forum - Learning Java easily</description>
		<language>en</language>
		<lastBuildDate>Mon, 20 May 2013 19:52:46 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.java-forums.org/images/misc/rss.jpg</url>
			<title>Java Programming Forum - Learn Java Programming - Blogs - Hibernate</title>
			<link>http://www.java-forums.org/blogs/hibernate/</link>
		</image>
		<item>
			<title>Packages, accessibility/visibility, synthetic access and protected constructors</title>
			<link>http://www.java-forums.org/blogs/hibernate/89-packages-accessibility-visibility-synthetic-access-protected-constructors.html</link>
			<pubDate>Fri, 12 Aug 2011 17:21:25 GMT</pubDate>
			<description>In Java it is common to use packages, especially for libraries. 
Two common packages, that are built in to Java, are java.io and java.util....</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">In Java it is common to use packages, especially for libraries.<br />
Two common packages, that are built in to Java, are java.io and java.util.<br />
(Another, even more common is java.lang, but that one is imported automatically.)<br />
<br />
Before you can use a package [without using the reflection classes] you most import it in<br />
the file (not class) that uses it. To import the package java.io, you add the line<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">import java.io.*;</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 just below your package line (if any; otherwise in the top of the).<br />
But if you only want to use java.io.File you add<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">import java.io.File;</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 instead.<br />
<br />
There is however another possible, although ugly (except with classes have the same name), way to<br />
using a class from another package: fully qualified class names.<br />
If you do not import the class, or package, you most write, for example:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">java.io.File file = new java.io.File(&quot;/&quot;);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Instead of:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">import java.io.File;
//&#91;&#8901;&#8901;&#8901;&#93;
File file = new File(&quot;/&quot;);</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Further reading: <a href="http://download.oracle.com/javase/1,5.0/docs/guide/language/static-import.html" target="_blank" rel="nofollow">Static Import</a><br />
<br />
<br />
Assume your Web site's address is <a href="http://www.example.org" target="_blank" rel="nofollow">www.example.org</a>, and your want to create a program called Game.<br />
It is then conventional to call your package (which may contain packages of its own) org.example.Game.<br />
All files in that package most have as its first non-comment/-empty line:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">package org.example.game;</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Your classpath is the folder where you put all your files and folders, in it you must now have<br />
the folder org/example/game, where all files in that package is located.<br />
The package file is located in is automatically imported, so you do not write:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">package org.example.game;
import org.example.game.*;</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 All packages are accessible form all packages, with the example of the default package.<br />
The default package is the nameless package located directly in your classpath, it is the<br />
package used when you do not specify package. The default package is not accessible from other packages.<br />
<br />
Further reading: <a href="http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html" target="_blank" rel="nofollow">Packages</a><br />
<br />
<br />
Accessibility (or visibility) of member is the property of from where a member is accessible/visible.<br />
An accessibility modifier is a (or none) keyword you add to a member (a class, interface, enum, method or variable).<br />
There are there such keywords: public, protected and private.<br />
The is also an accessibility used when not such keyword is used, the default accessibility or package private.<br />
public is the most visible, followed by protected, package private (default) and private.<br />
<br />
Public members are accessible from everywhere.<br />
Private members are only accessible from the same class/enum.<br />
Package private members are only accessible from the same package.<br />
Protect members are only accessible from the same package as well as from instances (non-static members) of subclasses.<br />
<br />
There is however a synthetic sugar called synthetic access. It also you to write code where<br />
a class's inner class access a private members of the outer class. This is used implicitly and<br />
you may get a warning from your compiler.<br />
<br />
<br />
Finally, there is an [synthetic salt] exception for the protected access modifier.<br />
Constructors with the access modifier protected cannot be access from subclass,<br />
but it can be used in the first line in the constructor, to specify which constructor should<br />
used.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">package superclass;
public class Superclass
{
    protected Superclass()
    {
        //&#91;&#8901;&#8901;&#8901;&#93;
    }
}

-------------------------------------

package subclass;
import superclass.*;
public class Subclass extends Superclass
{
    public Subclass()
    {
        super(); //Allowed &quot;use&quot; of superclass's protected constructor.
        Superclass superclass = new Superclass(); //Non-allowed use of the superclass's protected constructor.
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/89-packages-accessibility-visibility-synthetic-access-protected-constructors.html</guid>
		</item>
		<item>
			<title>Linux terminal magick, part 2</title>
			<link>http://www.java-forums.org/blogs/hibernate/88-linux-terminal-magick-part-2.html</link>
			<pubDate>Fri, 12 Aug 2011 03:05:44 GMT</pubDate>
			<description>In the previous part if ”Linux terminal magick” we show how to let use user use ^C and ^\ in the terminal, that was the last part of creating a...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">In the previous part if ”Linux terminal magick” we show how to let use user use ^C and ^\ in the terminal, that was the last part of creating a graphical terminal user interface. The parts are [almost] not ordered!<br />
<br />
For this part we add two more functions:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">/**
 * Sets the the value of the ECHO flag; iff on the input to the terminal will be echoed back while typing.
 * This can be used to hide what the user is type, for example when a password is requested.
 *
 * @param   on           Whether the flag should be on.
 * @throws  IOException  Should not be thrown in GNU.
 */
public static void setEchoFlag(final boolean on) throws IOException
{
    (new ProcessBuilder(&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;stty &quot; + (on ? &quot;echo&quot; : &quot;-echo&quot;) + &quot; &lt; &quot; + TerminalMagick.tty + &quot; &gt; /dev/null&quot;)).start();
}

/**
 * Sets the the value of the ICANON flag; iff on the input will wait to be sent onto a line feed is given.
 * This can be used to read single characters instead of lines.
 *
 * @param   on           Whether the flag should be on.
 * @throws  IOException  Should not be thrown in GNU.
 */
public static void setBufferFlag(final boolean on) throws IOException
{
    (new ProcessBuilder(&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;stty &quot; + (on ? &quot;icanon&quot; : &quot;-icanon&quot;) + &quot; &lt; &quot; + TerminalMagick.tty + &quot; &gt; /dev/null&quot;)).start();
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <br />
To your shutdown hook (or finally clause in the main method, as in part 1) you may want to add:<br />
TerminalMagick.setEchoFlag(true)<br />
TerminalMagick.setBufferFlag(true)<br />
<br />
<br />
TerminalMagick.setEchoFlag(true) will allow the user to see what he is writing, it terminal will print it out.<br />
TerminalMagick.setEchoFlag(false) will hide the typed text from the user, this is preferable if you are asking for a password. It is almost like press ScrollLock, except it will not become visible. It is also usable for graphical interfaces in the terminal.<br />
<br />
TerminalMagick.setBufferFlag(false) lets the program retrieve the typed character the instant it is typed, instead of when it user presses Enter.<br />
This is usable when you want to retrieve mouse clicks or single key presses. It is also usable if you want to create a method jumps to the last<br />
entry when the user presses the &#8593;-key, just like when you are not running a program in the console. But backspace, delete, the &#8592;-key and the &#8594;-key<br />
will lose there functionally, and which most be implemented by hand.<br />
TerminalMagick.setBufferFlag(true) will undo what is described above.<br />
<br />
<br />
In next part: Getting the width and height, of the terminal, character units.</blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/88-linux-terminal-magick-part-2.html</guid>
		</item>
		<item>
			<title>The Multiton pattern</title>
			<link>http://www.java-forums.org/blogs/hibernate/87-multiton-pattern.html</link>
			<pubDate>Fri, 12 Aug 2011 02:39:35 GMT</pubDate>
			<description>The Multiton pattern is a pattern missing from ”the pattern bible”. It is extensive to dual layer (and beyond), but that is left for you to use...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">The Multiton pattern is a pattern missing from ”the pattern bible”. It is extensive to dual layer (and beyond), but that is left for you to use intuitively when needed, it is not too difficult to do so.<br />
<br />
The Multiton pattern is an extension of Singleton pattern.<br />
Multiton lets you create and get any number of instances, they are distinguished by keys,<br />
so if you want instance &quot;a&quot; it creates it for you the first time, and returned the created instance the next time, but if you then want &quot;b&quot;, you get another instance.<br />
<br />
Multiton can be used for example when creating a skin (from files) for a window. Maybe you refer to the same component (perhaps a menu item) in multiple files, and maybe those are not even predefined.<br />
<br />
<br />
The following code is a template for Eclipse.<br />
${enclosing_type} is your class.<br />
${key_type} is the class used for keys.<br />
${hash_table_type} is the class you want to use for the hash table, maybe java.util.HashMap.<br />
<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">/**
 * Multiton constructor
 * 
 * @param   key  The key
 */
private ${enclosing_type}(final ${key_type} key)
{
    super();
}

/**
 * Gets (and may set) instance by key
 * 
 * @param   key  The key
 * @return       The instance
 */
public static ${enclosing_type} getInstance(final ${key_type} key)
{
    synchronized (instances)
    {
        ${enclosing_type} instance;

        if ((instance = instances.get(key)) == null)
        {
            instance = new ${enclosing_type}(key);
            instances.put(key, instance);
        }

        return instance;
    }
}

/**
 * The instances
 */
private static final ${hash_table_type}&lt;${key_type}, ${enclosing_type}&gt; instances = new ${hash_table_type}&lt;${key_type}, ${enclosing_type}&gt;();</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/87-multiton-pattern.html</guid>
		</item>
		<item>
			<title>The Singleton pattern</title>
			<link>http://www.java-forums.org/blogs/hibernate/86-singleton-pattern.html</link>
			<pubDate>Fri, 12 Aug 2011 02:29:16 GMT</pubDate>
			<description>In mathematics a singleton (not to be confused with a simpleton) is a set with exactly one element. 
In computer science Singleton is a pattern which...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">In mathematics a singleton (not to be confused with a simpleton) is a set with exactly one element.<br />
In computer science Singleton is a pattern which enforces the existence of only up to one instance of a class.<br />
<br />
Singleton is sometimes considered an anti-pattern, and it's famous for being considered unnecessary in the world of Python (where you duck type).<br />
Singleton is however useful, it is a nice and clean why to restrict the number of instances, to only one.<br />
In addition if gives you a simple way to get the instance without checking if the has been created yet, it is created, if it has not been created yet, when you create it.<br />
<br />
There is however a tendency amount people to make all classes Singleton as long as you do not need multiple instance, even in C#<br />
where adding the modifier static to the class makes it constructorless, just like adding a private nullary (takes not arguments)<br />
constructor [in Java too] as the only constructor. Making all classes Singleton is overkill, inflexible and makes the code extra messy,<br />
when you just can make static methods and, optionally, add a private nullary constructor.<br />
<br />
I recommend to use Singleton when and only when you need one instance, that is easily accessible in a simple/clean way, of the class,<br />
and when that instance must exist, for example, when the instance is a window, not a façade or logic class, only(?) for graphical objects.<br />
<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public final class YourClass //final is not required, it changes nothing because the only constructor is private
{
    /**
     * Singleton constructor
     */
    private YourClass() //There is a recommendation to use protected when working with JUnit (so you can use the constructor)
    {
        //Implement construction here
    }

    /**
     * Gets the only instance of this class
     *
     * @return  The only instance of this class
     */
    public static YourClass getInstance() //Use this method to get the instance of the class, instead of the constructor
    {
        if (onlyInstance == null)
            synchronized (YourClass.class)
            {
                if (onlyInstance == null)
                    onlyInstance = new YourClass();
            }

        return onlyInstance;
    }

    /**
     * The only instance
     */
    private static volatile YourClass onlyInstance = null;
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/86-singleton-pattern.html</guid>
		</item>
		<item>
			<title>Linux terminal magick, part 1</title>
			<link>http://www.java-forums.org/blogs/hibernate/85-linux-terminal-magick-part-1.html</link>
			<pubDate>Thu, 11 Aug 2011 23:15:37 GMT</pubDate>
			<description>Are you a hardcore GNU/Linux user like me? 
Then you are probably sick of Java (natively) not letting you print in the terminal with colours. 
In a...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Are you a hardcore GNU/Linux user like me?<br />
Then you are probably sick of Java (natively) not letting you print in the terminal with colours.<br />
In a series of Web logs, I will describe how to, in GNU/Linux, print in the terminal with colours and other stuff.<br />
Keep in mind that you need a TTY (e.g. gnome-terminal, xterm, Linux VT (linux)), Eclipse's built in console will not do.<br />
<br />
We will begin with letting Java accept ^C and ^\ (also know as ^4) from the terminal.<br />
<br />
First we note that if we start a process from Java it will not share tty with your program, so running &quot;echo `tty`&quot; will tell you that you are using a tty, even if your programming is using a tty.<br />
Linux have a character device symbolic link file named /dev/stdout, it is your tty. Printing to it will print to your terminal.<br />
Since it is a symbolic link we must get it target, its canonical path:<br />
tty = (new File(&quot;/dev/stdout&quot;)).getCanonicalPath();<br />
You can store tty where it suits you, it will not be modified.<br />
<br />
So in our class TerminalMagick we write (for clarity we will not use $NON-NLS comments, as used in Eclipse):<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">import java.io.*;

public class TerminalMagick
{
    /**
     * The stdout tty. This code assumes you have same stdin tty as stdout tty.
     */
    protected static String tty;

    /**
     * Class initialiser
     */
    static
    {
        //It is imported to use canonical path and not absolute path, otherwise it will change inode
        TerminalMagick.tty = (new File(&quot;/dev/stdout&quot;)).getCanonicalPath();
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 GNU have a nice program called stty, it will let you set various flags for your tty.<br />
On of those flags is isig, that lets you specify whether interruption signals should be send<br />
from the terminal when you press for example ^C.<br />
To tell stty to use your terminal we start it with: &lt; TerminalMagick.tty<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">import java.io.*;

public class TerminalMagick
{
    // &#91;&#8901;&#8901;&#8901;&#93;, the old stuff

    /**
     * Sets the the value of the ISIG flag; iff on signals may be cued from the TTY when the user presses special key combinations,
     * like ^C and ^\ (also know as ^4). The to 'false' to allow ^C (otherwise abort signal) and ^\(otherwise process dump (should actually be quit) signal).
     *
     * @param   on           Whether the flag should be on.
     * @throws  IOException  Should not be thrown in GNU.
     */
    public static void setInterruptionSignalFlag(final boolean on) throws IOException
    {
        (new ProcessBuilder(&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;stty &quot; + (on ? &quot;isig&quot; : &quot;-isig&quot;) + &quot; &lt; &quot; + TerminalMagick.tty + &quot; &gt; /dev/null&quot;)).start();
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <br />
Now, to let you use ^C and ^\ in your terminal (this will usable later, in another post) you can to this in your main method:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public static void main(final String... args)
{
    try
    {
        TerminalMagick.setInterruptionSignalFlag(false); //Lets you use ^C and ^\.

        //Do your normal stuff here…
    }
    finally //Runs when the try (and its catches) is complete, even if the method have returned
    {
        TerminalMagick.setInterruptionSignalFlag(true); //Resets ^C and ^\ to send signals (abort and quit).
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <br />
Stay tuned for [in the next part] password fields in the terminal (no echoing) and unbuffered input (each character send when typed, not on Enter)...</blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/85-linux-terminal-magick-part-1.html</guid>
		</item>
		<item>
			<title>Variadic functions (varargs)</title>
			<link>http://www.java-forums.org/blogs/hibernate/84-variadic-functions-varargs.html</link>
			<pubDate>Thu, 11 Aug 2011 22:47:19 GMT</pubDate>
			<description>Ever heard of variadic functions of varargs. 
It is my favourite synthetic sugar (although I must admit i prefer cane sugar). 
 
Variadic functions...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Ever heard of variadic functions of varargs.<br />
It is my favourite synthetic sugar (although I must admit i prefer cane sugar).<br />
<br />
Variadic functions have undefine arity (the parameter is called a &quot;vararg&quot; (variadic argument)), meaning<br />
that it lets you use any amount of parameters when invoking it.<br />
<br />
For example:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public class MyClass
{
    private static void print(final String prefix, final int... lines)
    {
        for (final int line : lines)
            System.out.println(prefix + String.valueOf(line));
    }

    private static void print(final String prefix, final String... lines) // (*)
    {
        for (final String line : lines)
            System.out.println(prefix + line);
    }



    public static void main(final String... args)
    {
        print(&quot;:&quot;, 1, 2, 3, 4, 5);
        print(&quot;:&quot;, new int&#91;&#93; {1, 2, 3, 4, 5});
        //print(&quot;:&quot;); //Would compile if we remove (*)
        print(&quot;:&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;);
    }

}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 int... is handled just like int[], except, it lets you specify each element in the array using a parameter instead of using an array, but it still lets you use an array.<br />
Varargs lets you use, between 0 and the maximum size of an array, parameters.<br />
It can be used alone unlike how it is used in the code above.<br />
<br />
One restriction with variadic functions is that you can only use up to one vararg, even if you want to use two mutually uncastable types.<br />
So you <b>cannot</b> write:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public class MyClass
{
    private static void print(final Strings... lines, final SomeOtherClass... moreLines)
    {
        for (final String line : lines)
            System.out.println(line);

        for (final SomeOtherClass line : moreLines)
            System.out.println(line.toString());
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 </blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/84-variadic-functions-varargs.html</guid>
		</item>
		<item>
			<title>Unusual members, part 2</title>
			<link>http://www.java-forums.org/blogs/hibernate/83-unusual-members-part-2.html</link>
			<pubDate>Thu, 11 Aug 2011 22:22:56 GMT</pubDate>
			<description>Just like there is a static, otherwise headless, method, the class initialiser, this is an object initialiser (or just initialiser), that is a...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Just like there is a static, otherwise headless, method, the class initialiser, this is an object initialiser (or just initialiser), that is a non-static and completely headless.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public class MyClass
{
    public static class Subclass extends MyClass
    {
        {
            System.out.println(&quot;Subclass Initialiser 1&quot;);
        }
        public Subclass()
        {
            System.out.println(&quot;Subclass Constructor&quot;);
        }
        {
            System.out.println(&quot;Subclass Initialiser 2&quot;);
        }
    }

    {
        System.out.println(&quot;Initialiser 1&quot;);
    }
    public MyClass()
    {
        System.out.println(&quot;Constructor&quot;);
    }
    {
        System.out.println(&quot;Initialiser 2&quot;);
    }



    public static void main(final String... args)
    {
        new Subclass();
        System.out.println(&quot;----&quot;);
        new Subclass();
    }

}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <br />
So want does this print?<br />
<br />
Well, the superclass is always constructed before the subclass, and initialisers called in order of appearance from the top down on object construction.<br />
Additionally initialisers are always called before the constructor, so the code prints:<br />
<br />
Initialiser 1<br />
Initialiser 2<br />
Constructor<br />
Subclass Initialiser 1<br />
Subclass Initialiser 2<br />
Subclass Constructor<br />
----<br />
Initialiser 1<br />
Initialiser 2<br />
Constructor<br />
Subclass Initialiser 1<br />
Subclass Initialiser 2<br />
Subclass Constructor</blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/83-unusual-members-part-2.html</guid>
		</item>
		<item>
			<title>Unusual members, part 1</title>
			<link>http://www.java-forums.org/blogs/hibernate/82-unusual-members-part-1.html</link>
			<pubDate>Thu, 11 Aug 2011 22:09:53 GMT</pubDate>
			<description>Did you ever want to run something the first, and only first, time a class was being used. 
Perhaps wanted to do some complex calculations and assign...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Did you ever want to run something the first, and only first, time a class was being used.<br />
Perhaps wanted to do some complex calculations and assign it to a constant.<br />
<br />
One way to do this is by writing, for example:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public class YourClass
{
    public static final int YOUR_CONSTANT = constantValue();

    private static int constantValue()
    {
        int value1 = 0, value2 = 1;
        int value = 0;
        for (int i = 0; i &lt; 10; i++)
        {
            value = value1 + value2;
            value2 = value1;
            value1 = value;
        }
        return value:
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 All static variables are assigned direct when the class is first being used,<br />
so if you want to print &quot;Hello&quot;, you could do so in a method used by a dummy variable.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public class YourClass
{
    private static final Void IM_A_DUMMY = printHello();

    private static Void printHello()
    {
        System.out.println(&quot;Hello&quot;);
        return null;
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 But there is a much prettier way to do this, especially for more complex code: by using a class initialiser:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public class MyClass
{
    private static final Void DUMMY = dummysCode();
    public static final int VALUE;

    /**
     * Class initialiser
     */
    static
    {
        System.out.println(&quot;Hello&quot;);
        VALUE = 10;
    }

    private static final Void ANOTHER_DUMMY = anotherDummysCode();

    static
    {
        System.out.println(&quot;Initialised!&quot;);
    }



    private static Void dummysCode()
    {
        System.out.println(&quot;Dummy's code&quot;);
        return null;
    }

    private static Void anotherDummysCode()
    {
        System.out.println(&quot;Another dummy's code&quot;);
        return null;
    }



    public static void main(final String... arg)
    {
        System.out.println(VALUE);
    }

}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Running main() will print:<br />
Dummy's code<br />
Hello<br />
Another dummy's code<br />
Initialised!<br />
10<br />
<br />
Variable as assigned from the top down, running any class initialiser in the way.</blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/82-unusual-members-part-1.html</guid>
		</item>
		<item>
			<title>Legal forward reference assignment weirdness on declarations</title>
			<link>http://www.java-forums.org/blogs/hibernate/81-legal-forward-reference-assignment-weirdness-declarations.html</link>
			<pubDate>Thu, 11 Aug 2011 21:38:38 GMT</pubDate>
			<description><![CDATA[Observer the glorious code by your's truly Hibernate: 
 
<div class="bbcode_container"> 
	<div class="bbcode_description">Java Code: </div> 
 
	<pre...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Observer the glorious code by your's truly Hibernate:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: </div>

	<pre class="brush: java">public class YetAnotherClass
{
    //public static final int YET_ANOTHER_CONSTANT = YET_ANOTHER_CONSTANT; //Compile-time error: illegal forward reference
    public static final int ANOTHER_CONSTANT = YetAnotherClass.ANOTHER_CONSTANT; //OK!

    //public int yetAnotherVariable = yetAnotherVariable; //Compile-time error: illegal forward reference
    public int anotherVariable = this.anotherVariable + 1; //OK!


    public static void main(final String... args)
    {
        System.out.println(ANOTHER_CONSTANT);
        System.out.println(new YetAnotherClass().anotherVariable);
    }
}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Obviously you can not assign a variable to the value of it self before it is declared, hence you<br />
can not write the two commented lines. But if you refer to them in a fully qualified manner, you can!<br />
<br />
It is not a bug, it goes hand in hand with the Java specifications.<br />
<br />
If you run this code, it prints:<br />
0<br />
1<br />
<br />
When value of it the referred to variable is 0, false or null (the default value), if it has not yet been declared.</blockquote>

]]></content:encoded>
			<dc:creator>Hibernate</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/hibernate/81-legal-forward-reference-assignment-weirdness-declarations.html</guid>
		</item>
	</channel>
</rss>
