What is Reference Variable ?
Ans: A Reference variable is variable which holds the address of a Object of any Class.
Actually Speaking this Variable will hold two Parts of addresses.
Part-1 : Will hold the address of the Object .
Part-2 : Will hold the address of context.
What is Address of Context and What is address of Object?
Ans : Take a Sample Program
Class Example
{
int i;
static int j;
public static void main(String args[]){
Example object = null;
System.out.println("I variable : "+object.i); /* Prints Error */
System.out.println(" Static J Variable Value "+object.j) /* prints j = 0 */
}
}
Observation : Here the Reference variable object .
it holds the address of the Object, in this case it is "null".
and it holds the address of the context(static) i.e., it prints context value j =0;
Address of a Context for a Reference variable will be assigned by the JVM and it also once.
Address of a object for a Reference variable will be program(programmer) we can assign null or some address.
Very Important point about the static and non static memory blocks.
Static functions and static variables would be available to non-static functions of the same class. But converse is not TRUE.
So to access non-static variables / functions from static functions we need OBJECT
so Only Problem is that We cannot access a Non static members and Functions from a Static context , we can access Only through the OBJECTS.
Simply, Context of a Class is nothing but Static Context.
How to access the context of a Class[nothing but static context] from another another class?
Answer : Static context or context of class can be accessed from anywhere just simply specifying the name of the Class.
How to access the non static method using the Context of class as you know from the context of a class we can access only the static content.
Answer: Using a static Object .
Below is the Example :
class Second {
int instanceVariable = 900;
static int staticVariable = 400;
public void nonStaticMethod(){
System.out.println("Non Static Method");
}
public static void staticMethod(){
System.out.println("Static Method ");
}
static Third t = new Third();
}
class Third{
int i = 300;
int j = 800;
public void functionA(){
System.out.println(" Function A");
}
public static void staticMethod(){
System.out.println(" Inside Static Method Of class Third");
}
}
class First {
public static void main(String args[])
{
Second.t.functionA(); /* we are calling a non static method functionA using a static Object of Class Third
}
}
Best Example is
System.out.println();
Where : println is non static method of class printstream.
out is is a static object of class printstream
Where System is another class which created the static object ( out) for the class Printsream.
Hi, This blog is the place who want to prepare for programming interview with major product development companies. Apart from interview questions I would like to provide some concepts on the new technologies which I learn as part of my carrier. Thanks for visiting my Blog.
Saturday, October 16, 2010
Subscribe to:
Post Comments (Atom)
AWS certification question
AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...
-
Here I am going to discuss some of the differences between Text and String class in Hadoop. Text class lies in the package: import org.ap...
-
Q: What is the library you used in spring to connect to database? Spring JDBC template. Q: What and all involved in fetching the data ...
-
There are 2 ways we can load data into the HIVE Tables. In this tutorial we are loading data into the HIVE managed Table. This is my sa...
No comments:
Post a Comment