Search More About Above Topic Here

Custom Search

What happens if your serializable class contains a member which is not serializable in Java?

Links to this post
It is simple, the class will not be serializable, unless the field is declared transient (which will prevent the field from being serialized, i.e. it will not be saved).

It'll throw a NotSerializableException when you try to serialize it. To avoid that, make that field a "transient" field.

private transient YourNonSerializableObject dontSerializeMe; // This won't be serialized

What is "ThreadLocal" in Java?

Links to this post
ThreadLocal class simplifies the development of thread-safe concurrent programs. It ensures the object stored in this class not shared between threads. It is as an alternative to synchronization, to improve scalablity in transaction-intensive environments. Classes encapsulated in ThreadLocal are automatically thread-safe in a pretty simple way, since  it's clear that anything stored in ThreadLocal is not shared between threads.

How to make an object immutable in Java? What are the benefits?

Links to this post
By following steps we can create an immutable object.
  •  Declare the class as 'final' . This would prevent any other class from extending it and hence from overriding any method from it which could modify instance variable values.
  • Do not provide setter methods to modify values of any of the instance variables of the class.
  • Make all the members or instance variable as private and final, set them explicitly, in a static block, or in the constructor.
  • You can also declare the constructor of the class as private and add a factory method to create an instance of the class when required.
  • Make all the members private.
  • No Methods that modify state.
  • Object reference should not be leaked during construction phase
Be extremely careful to limit access to mutable members(remember the field may be final but the object can still be mutable. ie private final Date imStillMutable). You should make defensive copies in these cases.

The reasoning behind making the class final is very subtle and often overlooked. If its not final people can freely extend your class, override public or protected behavior, add mutable properties, then supply their subclass as a substitute. By declaring the class final you can ensure this won't happen.

Benefits:
  • simple to construct
  • automatically thread safe
  • good candidate for Map keys and Set as their internal state would not change while processing
  • don't need implementation of clone as they always represent same state

When to use "this" keyword in Java?

Links to this post
"this" keyword plays an important role in Java to reduce the number of lines code and performance. You can observe the following important uses of "this" keyword.
  • To refer current class instance variables.
  • To invoke current class method.
  • To invoke current class constructor.
  • To return the current class instance.

Example 1: 

class Employee{
int employeeId;
String employeeName;

Employee(int employeeId,String employeeName){
this.employeeId= employeeId;
this.employeeName= employeeName;
}
void display(){
      System.out.println(employeeId+" "+employeeName);
}
public static void main(String args[]){
               Employee e1 = new Employee (111,"John");
               Employee e2 = new Employee (222,"Abraham");
               e1.display();
               e2.display();
    }
}

What do you need to do to run a class with a main() method in a package?

Links to this post
Example: Say, you have a class named "Pet" in a project folder "c:\myProject" and package named com.xyz.client, will you be able to compile and run it as it is?

package com.xyz.client;

public class Pet {

public static void main(String[] args) {

System.out.println("I am found in the classpath");

}

}

To run �� c:\myProject> java com.xyz.client.Pet

Custom Search
Related Posts with Thumbnails