Author: usenkanov

Java: final keyword and why inner classes cannot access non-final variables of an outer class

“Final” keyword in Java can be used in four contexts:

Final class – class with all the methods implicitly final, but not necessarily data members. Cannot be extended.
Final method – method that cannot be overridden.
Final primitive data members (basically primitives within the class) – cannot be reassigned once they are assigned a value.
Final object data members (basically objects within the class, eg: String, Vector…) – cannot be reassigned once they are assigned a value. However, if they are mutable they can be mutated.

Why inner classes cannot access non-final variables of an outer class in Java?

The full story is here: http://www.smotricz.com/kabutz/Issue025.html (overall an interesting newsletter, classic)
So basically the problem is because Java doesn’t use pointers as C,C++ do.
Hence, whenever class (with inner class in it) is compiled the following occurs:
1. outer class is passed as a constructor argument to the inner class
2. in special case when value of the local variable (let’s say X) can change for different instances of the inner class, compiler passes it as a constructor argument to the inner class. As a result, a new variable is created which can be changed independently in each inner class instance as well as in outer class, which shouldn’t be allowed. Hence, we have those limitations.
Hence, consider this:
public class Access2 {
    public void f() {
      for (int i=0; i<10; i++) {
	final int value = i;
	Runnable runnable = new Runnable() {
	  public void run() {
	    System.out.println(value);
	  }
	};
      }
    }
  }
Now each Runnable instance can reference variable i without any fear, because it is final and won’t change.
Overall, I wouldn’t advise using inner classes at all. I rarely find any real benefit in them.
Is it so hard to create a new class file? 🙂

Java: “transient” variables

From the Java Language Specification:

Variables may be marked transient to indicate that they are not part of the persistent state of an object.

For instance, fields in the class that can be derived from other fields in the class. Good example would be to save Image objects as transient object. When object is being de-serialized transient fields usually need to be reinitialized.

Eg:

private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException
{         
 inputStream.defaultReadObject();         
 generateImage(); 
}   

In this example generateImage method needs to be called to read the image based on some non-transient variables (such as path to the image) 🙂

J2EE: APIs

JTA – Java Transaction API – allows multiple transactions (thus, distributed) to be performed on more than one XA resources in Java environment.

Note: XA describes the interface between the global transaction manager and the local resource manager. The goal of XA is to allow multiple resources (such as databases, application servers, message queues, transactional caches, etc.) to be accessed within the same transaction, thereby preserving the ACID properties across applications. XA uses a two-phase commit to ensure that all resources either commit or rollback any particular transaction consistently (all do the same).

JTS – Java Transaction Service – is a specification for building a transaction manager that maps onto the Object Management Group’s (OMG) Object Transaction Service (OTS) used in the CORBA (Common Object Request Broker Architecture) architecture.

* note: Due to late criticism and decline in popularity of CORBA (WebServices are getting more and more popular) JTS is primarily used within J2EE architecture and rarely supports integration with other languages/architectures.

More information:

http://www.ibm.com/developerworks/java/library/j-jtp0305/index.html

http://www.ibm.com/developerworks/java/library/j-jtp0410/index.html

JAAS –  Java Authentication and Authorization Service – provides a way for a J2EE application to authenticate and authorize a specific user or group of users to run it. It is a standard Pluggable Authentication Module (PAM) framework that extends the Java 2 platform security architecture to support user-based authorization.

Cloud types

SaaS – software as a service:

–          “on-demand” delivery model for business applications

–          has been incorporated into the strategy of all leading enterprise software companies

–          has been popularized by salesforce.com

IaaS – Infrastructure as a service

–          eg: Amazon Cloud` services

PaaS – Platform as a service

–          eg: Google Web Toolkit

Memory allocation and overflows outlined

Program can store its data in 3 places:

– data area (contains only static variables)

– stack (contains local variables and return addresses when subroutine is called)

– heap (dynamically allocated memory, objects are allocated here due to their size)

Buffer overflow:

– Stack overflow

Limited amount of memory (usually determined when program starts)

Usually caused by 2 errors:

– infinite recursion

– very large stack variables (usually creating too large local variables, therefore arrays larger than a few KB should be allocated dynamically)

ex:

int foo() {

double x[1000000];

}

* when a single-threaded program runs as a multi-threaded, it will be allocated less space per thread. Therefore program that runs OK might crush when multi-threaded.

– Heap overflow

Memory is allocated dynamically

Primarily program DATA is contained

* on average, half of all critical security leaks are based on heap overflows (ex: iPhone, PS3 homebrews)

* more information: http://www.h-online.com/security/features/A-Heap-of-Risk-747161.html