Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts

Friday, 13 January 2012

10 Example of this keyword in Java

this keyword in Java is a special keyword which can be used to represent current object or instance of any class in Java. “this” keyword can also call constructor of same class in Java and used to call overloaded constructor. In this Java tutorial we will see how to use this keyword in Java  and
different examples of this in Java. this sometime also associate with super keyword which is used to denote instance of super class in Java and can be used to call overloaded constructor in Java.

1) this keyword represent current instance of class.

2) You can synchronize on this in synchronized block in Java

synchronized(this){
//this synchronized block will be locked on current instance
}

3) this keyword can be used to call overloaded constructor in java. if used than it must be first statement in constructor this() will call no argument constructor and this(parameter) will call one argument constructor with appropriate parameter. here is an example of using this() for constructor chaining:

Example of this keyword to call constructor in Java

class Loan{
    private double interest;
    private String type;
 
    public Loan(){
       this(“personal loan”);
    }
 
    public Loan(String type){
        this.type = type;
        this.interest = 0.0;
    } 
}

4) If member variable and local variable name conflict than this can be used to refer member variable.
here is an example of this with member variable:

  public Loan(String type, double interest){
        this.type = type;
        this.interest = interest;
  }

Here local variable interest and member variable interest conflict which is easily resolve by referring member variable as this.interest
5) this is a final variable in Java and you can not assign value to this. this will result in compilation
error:

this = new Loan(); //cannot assign value to final variable : this

6) you can call methods of class by using this keyword as shown in below example.
  
   public String getName(){
        return this.toString();
    }

7) this can be used to return object. this is a valid return value. here is an example of using as return value.

public Loan getLoan(){
 return this;
}

8) this can be used to refer static members in Java as well but its discouraged and as per best practices
this should be used on non static reference.

9) "this" keyword can not be used in static context i.e. inside static methods or static initializer block.
if use this inside static context you will get compilation error as shown in below example:

  public static void main(String args){
       this.toString{}; //compilation error: non static variable this can not be used in static context
  }

10) this can also be passed as method parameters since it represent current object of class.


That’s all on this keyword in Java. You should know about this and super keyword and get yourself familiar with different usage of this keyword in java.

How to get current Date Timestamps in Java on GMT or Local Timezone Example

Getting current data and timestamps in java is requirement for most of java enterprise application. Formatting date in java or parsing date in Java is such a common functionality that you will need it now and than. Though most of programmer look Google for finding current date and timestamps code in Java, its worth to having an idea how you can do that by using java.util.Date class. some time you might need formatted data string say "yyyy-MM--dd:HH:mm:ss". That is also possible by using dateformat in Java. I have touched base on string to date conversion and now in this article we will see that in detail in terms of current date, time-stamp and timezone values. if you are working in global core java application which runs on different timezones you must need to include timezone in your date and timestamp to avoid any confusion.

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More