Search More About Above Topic Here

Custom Search

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

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, will you be able to compile and run it as it is?


public class Pet {

public static void main(String[] args) {

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



To run �� c:\myProject> java

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

Links to this post
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.

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?

Links to this post
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 is 1 for thread1 and 2 for thread2 but these values are not distributed to main memory or other threads.

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

Links to this post
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.

Custom Search
Related Posts with Thumbnails