just staff i don't wanna forget

18 Ekim 2014 Cumartesi

Anonymous Classes in JAVA

17:08 Posted by Burhan ARAS , No comments
What does this mean?
An anonymous class is just what its name imples -- it has no name. It combines the class declaration and the creation of an instance of the class in one step.
Probably most of the classes you have developed so far have been named classes such as Queue or Circle. Each of these classes contained a full definition; you could instantiate objects from any of these classes.
When defining an anonymous class, the definition and object instantiation is done all in one step. The class itself has no name (this is why it's anonymous) and this is the only way an object can be instantiated.
Since anonymous classes have no name, objects can not be instantiated from outside the class in which the anonymous class is defined. In fact, an anonymous object can only be instantiated from within the same scope in which it is defined.
Why use an anonymous class?
Anonymous classes can be time-savers and reduce the number of .java files necessary to define an application. You may have a class that is only used in a specific situation such as a Comparator. This allows an "on the fly" creation of an object.
You may find you prefer to use anonymous classes; many people use them extensively to implement listeners on GUIs.
What are these classes with unusual names?
Just because a class is anonymously defined, it's still a class and a corresponding class file needs to be created.
When you compile a class that contains an anonymous class definition, the compiler will create separate class files named
    ClassName$SomeNumber
where ClassName is the name of the class in which this anonymous class is defined, and SomeNumber is a the sequence number for the anonymous class.
In other words, if you implement a single anonymous class within the class MyAnonymousTest, the compiler will generate the files MyAnonymousTest.class and MyAnonymousTest$1.class.
If you implement two anonymous classes in the same class, MyAnonymousTest$2.class will also be created.
The Syntax for Anonymous Class Definitions
This example demonstrates an anonymous class definition for a comparator that is passed to the sort() method in the Collections class. Assume that aList is a valid List of data that is to be sorted.
    Collections.sort (aList,
      new Comparator () { // implements the IF
        public int compare (ObjectType o1, ObjectType o2 ) throws ..{
          .... implementation for compare() 
        } // end of compare() 
      } // end of Comparator implementation
    ); // closed paren for sort() and end of statement semicolon

Rules:

      An anonymous class must always extend a super class or implement an interface but it cannot have an explicit extends or implements clause.
      An anonymous class must implement all the abstract methods in the super class or the interface.
      An anonymous class always uses the default constructor from the super class to create an instance.

Garbage Collector

14:41 Posted by Burhan ARAS , , No comments
What allows the programmer to destroy an object x?
A.x.delete()
B.x.finalize()
C.Runtime.getRuntime().gc()
D.Only the garbage collection system can destroy an object.
Answer: Option D
Explanation:
Option D is correct. When an object is no longer referenced, it may be reclaimed by the garbage collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that would not otherwise be released. When a class is no longer needed, it may be unloaded.
Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are:
  1. delete() - Method in class java.io.File : Deletes the file or directory denoted by this abstract pathname.
  2. delete(int, int) - Method in class java.lang.StringBuffer : Removes the characters in a substring of this StringBuffer.
  3. delete(int, int) - Method in interfacejavax.accessibility.AccessibleEditableText : Deletes the text between two indices
  4. delete(int, int) - Method in class :javax.swing.text.JTextComponent.AccessibleJTextComponent; Deletes the text between two indices
None of these destroy the object to which they belong.
Option B is wrong. I found 19 finalize() methods. The most interesting, from this questions point of view, was the finalize() method in class java.lang.Objectwhich is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method does not destroy the object to which it belongs.
Option C is wrong. But it is interesting. The Runtime class has many methods, two of which are:
  1. getRuntime() - Returns the runtime object associated with the current Java application.
  2. gc() - Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects. Interesting as this is, it doesn't destroy the object.

Access Modifiers

14:36 Posted by Burhan ARAS No comments
Access modifiers dictate which classes, not which instances, may access features.
Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way.
private makes a member accessible only from within its own class
protected makes a member accessible only to classes in the same package or subclass of the class
default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package.
public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible)
final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised
abstract declares a method that has not been implemented.
transient indicates that a variable is not part of the persistent state of an object.
volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable.

Access Modifiers

12:17 Posted by Burhan ARAS No comments
Access modifiers dictate which classes, not which instances, may access features.
Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way.
private makes a member accessible only from within its own class
protected makes a member accessible only to classes in the same package or subclass of the class
default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package.
public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible)
final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised
abstract declares a method that has not been implemented.
transient indicates that a variable is not part of the persistent state of an object.
volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable.

1 Ağustos 2013 Perşembe

Developerlardan Özlü Sözler

14:46 Posted by Burhan ARAS No comments
Object-Oriented ustasıyım, Design Pattern hastasıyım.

 Eşin, benzerin yok bu alemde. Singleton musun güzelim? (sevgiliye hoş bir söz...)

 Aşkımızı Microsoft'a anlattım, .Net 4.0 diye release oldu.

 Java'da pointer olsaydı, yalnız seni gösterirdi.

 Mesele nesneye yönelmek değil yeğen! Mesele nesneyi yöneltebilmekte!

 Hayatta herşey bir pattern olsa, senin adın Singleton olurdu. 

Testerla dargın, çilekeş developer!

 Gözlerine her bakışımda, kalbimden bir exception fırlatılır.

5 Nisan 2012 Perşembe

A Printer class for GWT

01:00 Posted by Burhan ARAS , , No comments

import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.Element;

public class Printer {
    public static native void it() /*-{
        $wnd.print();
    }-*/;
    public static native void it(String html) /*-{
        var frame = $doc.getElementById('__printingFrame');
        if (!frame) {
            $wnd.alert("Error: Can't find printing frame.");
            return;
        }
        frame = frame.contentWindow;
        var doc = frame.document;
        doc.open();
        doc.write(html);
        doc.close();
        frame.focus();
        frame.print();
    }-*/;
    public static void it(UIObject obj) {
        it("", obj.getElement().toString());
    }
    public static void it(Element element) {
        it("", element.toString());
    }
    public static void it(String style, String it) {
        it("<it><header>"+style+"</header><body>"+it+"</body></it>");
    }
    public static void it(String style, UIObject obj) {
        it(style, obj.getElement().toString());
    }
    public static void it(String style, Element element) {
        it(style, element.toString());
    }
}

Open a new window in GWT

00:59 Posted by Burhan ARAS , , No comments

public static void openNewWindow(String name, String url) {
   com.google.gwt.user.client.Window.open(url, name.replace(" ", "_"),
          "menubar=no," +
          "location=false," +
          "resizable=yes," +
          "scrollbars=yes," +
          "status=no," +
          "dependent=true");
}