~~ CORE-JAVA ~~

 1. INTORDUCTION

     1.1 Java Intorduction
     1.2 Java history
     1.3 Java features
     1.4 Diff b/w Java & C++
     1.5 Java Variables
     1.6 Java Data Types
     1.7 Typecasting
     1.8 Arrays

 2. OOPS

     2.1 Introduction
     2.2 Class
     2.3 Object
     2.4 Static Keywords
     2.5 Constructors
     2.6 This Keyword

 3. INHERITANCE

     3.1 Inheritance (IS-A) 
     3.2 Aggregation (HAS-A)

 4. POLYMORPHISM

     4.1 Polymorphism & Its Type
     4.2 Method Overloading
     4.3 Method Overriding
     4.4 Super Keyword
     4.5 Final Keyword
     4.6 Dynamic Binding

 5. ABSTRACTION

     5.1 Abstract Classes & Methods
     5.2 Interfaces
     5.3 Abstract vs Interface

 6. ENCAPSULATION

     6.1 Encapsulation

 7. STRING MANIPULATIONS

     7.1 String
     7.2 String Buffer
     7.3 String Tokenizer

 8. PACKAGES

     8.1 Predefined packages
     8.2 Userdefined Packages
     8.3 Access Specifiers

 9. EXCEPTION HANDLING

     9.1 Introduction
     9.2 Pre Defined Exceptions
     9.3 Try-Catch-Finally
     9.4 Throws, throw
     9.5 User Defined Exception

 10. MULTITHREADING

     10.1 Introduction
     10.2 Thread Creations
     10.3 Thread Life Cycle
     10.4 Life Cycle Methods
     10.5 Synchronization
     10.6 Wait() notify() notify all()

 11. WRAPPERCLASSES

     11.1 Introduction
     11.2 Wrapper Classes

 12. COLLECTION FRAME WORK

     12.1 Introduction
     12.2 List interface
     12.3 Set interface                                 12.4 Map interface                           

 13. INNER CLASSES

     13.1 Introduction
     13.2 Member inner class
     13.3 Static inner class
     13.4 Local inner class
     13.5 Anonymous inner class

 14. CoreJava Interview Questions

Java | this Keyword :

In Java, whenever we want to refer current Class object then we should go for this Keyword. We can use this Keyword for :

    • To refer instance variable of Current Class.
    • To initialize Current Class constructor also.
    • To pass value as an argument in the constructor call.
    • To pass value as an argument in the method call.
    • To return Current Class instance.

 

1. To Refer Instance Variable Of Current Class :

Sometimes if instance variable name and parameter name both are same then it creates an problem. Here we can use this keyword to resolve this issue. Now here we will see an example how we can refer instance variable of Current Class.

Example :

public class Book {

int id;
String name;
String description;

//Using this keyword to refer instance variable
Book(int id, String name) {
this.id = id;
this.name = name;
}

void display() {
System.out.println("BookId is : " + id + " and Book Name is : " + name);
}

public static void main(String[] args) {

Book b1 = new Book(100, "Java");
b1.display();

}
}

Output : 

BookId is : 100 and Book Name is : Java

 

Note : Here local variable and instance variable both are same so we are using this keyword, but if local variable and instance variable are different then no need to use this keyword.

2. To Initialize Current Class Constructor :

Here we will see, how we can call current class constructor using this keyword, we are creating simple program where we will call default constructor from parameterized constructor using this keyword.

Example :

public class Book {

Book() {
System.out.println("In default constructor");
}

// Calling default constructor from parameterized constructor
Book(int id) {
this();
System.out.println("BookIs is : " + id);
}

public static void main(String[] args) {

Book b = new Book(100);

}
}

Output :

In default constructor
BookIs is : 100

 

3. To Pass Value As an Argument in the constructor Call :

We can also use this keyword to pass an argument in constructor call, Below is simple example how to pass that argument.

Example :

public class Student {

Book obj;

Student(Book obj){
this.obj=obj;
}

void display() {
System.out.println("BookName is "+ obj.name);
}
}



public class Book {

String name = "Java";

//To pass argument in constructor call
Book() {
Student student = new Student(this);
student.display();
}

public static void main(String[] args) {

Book b = new Book();

}
}

Output :

BookName is Java

 

4. To pass value as an argument in the method call :

We can also use this keyword to pass an argument in the method call. This type of implementation we will see mainly in event handling. Below is simple example where we are passing argument in method call.

Example :

public class Book {

void myBook(Book obj) {
System.out.println("myBook method is calling.");
}

//Passing value as argument in method using this keyword
void my() {
myBook(this);
}

public static void main(String[] args) {

Book b = new Book();
b.my();

}
}

Output :

myBook method is calling.

 

5. To Return Current Class Instance.

sdsd