Skip to main content

Posts

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

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?

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.

When to use "this" keyword in Java?

"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();           ...

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

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

Java.Warning - Build path specifies execution environment J2SE-1.6

When you creat a Java Maven Project in the Eclipse, your Eclipse may show the following warning " Build path specifies execution environment J2SE-1.6. There are no JREs installed in the workspace that are strictly compatible with this environment. " To get resolve from this project setup issue by following steps. Solution: Go to your project   ---> Properties ---> Java build path : Libraries --->Remove the "JRE System Library[J2SE 1.6]" ---> Click "Add Library" button -> JRE System Library ---> Select the new "Executin Environment" or Workspace default JRE Hope your problem is solved now.

What is the diffrence between "volatile" and "synchronized" keywords?

Volatile keyword is thought of as an easy form of synchronized. It require less coding than synchronized and frequently have less run-time overhead. When we talk about locks, 2 main features introduce: i.) mutual exclusion which means only one thread at a time can hold a given lock, and ii.) visibility which ensures that changes mode to shared data before releasing the lock are accessible to another thread that subsequently acquires the lock. Comparing the volatile keyword with synchronized, the volatile is a field modifier while synchronized is used with code blocks and methods. Let's clear one more point here. Threads can have local copies of variables/data which don't have to be the same as the data held in other threads. In fact, java has an idea of a MAIN memory which holds the current value for variables. And threads can have their own copy of variables which can be different from the main memory. And it's possible that suppose you have a variable named X which value...

Can 'transient' variables be declared as 'final' or 'static'?

Java's serialization provides an elegant, and easy to use mechanism for making an object's state persistent. While controlling object serialization, we might have a particular object data member that we do not want the serialization mechanism to save. The modifier transient can be applied to field members of a class to turn off serialization on these field members. Every field marked as transient will not be serialized. You use the transient keyword to indicate to the Java virtual machine that the transient variable is not part of the persistent state of an object. The transient modifier applies to variables only. Like other variable modifiers in the Java system, you use transient in a class or instance variable declaration like this: class TransientExample { transient int hobo; . . . } This statement declares an integer variable named hobo that is not part of the persistent state of the TransientExample class.