QA's approach 2 Java- This & Super Keywords
This & Super Keywords
Example 2:
What is an instance variable ?
A variable that is declared in the class is known as instance variable.Every object of that class will contain a copy of the variable.
Example:
class Code
{
int var=10;//instance variable
methodA()
{
----
---
}
}
Example:
import java.io.*;
class Code
{
int var=15;
methodA()
{
int var=5;
System.out.println("Local variable is"+var);
System.out.println("Instance variable is"+this.var);
}
}
class MainClass
{
public static void main(String args[])
{
Code c=new Code();
c.methodA();
}
}
Output:
Local variable is 5
Instance variable is 15
Example:
import java.io.*;
class Code
{
void methodA()
{
System.out.println("Im in methodA of Code Class");
this.methodB();
}
void methodB()
{
System.out.println("Im in methodB of Code Class");
}
}
class MainClass
{
public static void main(String args[])
{
Code c=new Code();
c.methodA();
}
}
Output :
Im in methodA of Code Class
Im in methodB of Code Class
Example:
import java.io.*;
class Code
{
Code()
{
this(5);
System.out.println("inside the constructor without parameters");
}
Code(int a)
{
System.out.println("Inside the overloaded constructor with int value "+a);
}
public static void main(String args[])
{
Code c=new Code();//default constructor is called
}
}
Output :
Inside the overloaded constructor with int value 5
inside the constructor without parameters
this :
this is a reference to the current class object.this can be used with respect to variables,methods and classes.
It is a keyword.
It is a pre-defined non-static variable
It holds object reference.
It must be used inside the non-static context only.
Example 1
class Demo
{
static
{
System.out.println(this); //Compiler error.....
}
{
System.out.println(this); //No error, "this" can be used inside the non-static context....
}
}
Question : How can we access object functionality from non-static context?
Answer : using “this”
Question : How can we access object functionality from static context?
Answer : using “user defined object reference variables”
Example 2:
class Demo
{
Demo()
//non-static context
{
System.out.println(this);
//it prints object reference value in Hexa string format
}
public
static void main(String args[ ])
{
new
Demo( );
}
}
Note : Generally an Object reference is +ve
integer value. but that will be converted implicitly into hexa string format.
this with respect to variables:
this keyword is used to preserve the values of the instance variables.What is an instance variable ?
A variable that is declared in the class is known as instance variable.Every object of that class will contain a copy of the variable.
Example:
class Code
{
int var=10;//instance variable
methodA()
{
----
---
}
}
Example:
import java.io.*;
class Code
{
int var=15;
methodA()
{
int var=5;
System.out.println("Local variable is"+var);
System.out.println("Instance variable is"+this.var);
}
}
class MainClass
{
public static void main(String args[])
{
Code c=new Code();
c.methodA();
}
}
Output:
Local variable is 5
Instance variable is 15
this with respect to methods:
As a general procedure,If we want to access a particular method, we create an object to that class and access the desired method. In this process 'this' is used implicitly.Example:
import java.io.*;
class Code
{
void methodA()
{
System.out.println("Im in methodA of Code Class");
this.methodB();
}
void methodB()
{
System.out.println("Im in methodB of Code Class");
}
}
class MainClass
{
public static void main(String args[])
{
Code c=new Code();
c.methodA();
}
}
Output :
Im in methodA of Code Class
Im in methodB of Code Class
this with respect to constructors:
this keyword is used within a constructor to invoke another overloaded constructor.Usually this is placed in the first line of the calling constructor.Example:
import java.io.*;
class Code
{
Code()
{
this(5);
System.out.println("inside the constructor without parameters");
}
Code(int a)
{
System.out.println("Inside the overloaded constructor with int value "+a);
}
public static void main(String args[])
{
Code c=new Code();//default constructor is called
}
}
Output :
Inside the overloaded constructor with int value 5
inside the constructor without parameters
Comments
Post a Comment