Saturday, December 11, 2010

when object is created when refrence is assigned???

*Remeber object is always created when you declare an object of class type and refrence is always assigned when u called constructor of that class.
for example

class welcome{
private String greet;
public welcome()
{
greet="Welcome to Java......................!!!!";
}
public void displayGreeting()
{
System.out.println("Greeting :   "+greet);
}
}
public class Demo
{
public static void Main(String[] args)
{
1) welcome w;          
2) w.displayGreeting();           //generates an error of uninitialized object but the matter it has no refrence and                  dont know where to point.
3) welcome wel=new welcome();         //object type :welcome and refrence of:welcome
4) wel.displayGreeting();
}
}
*Remember statment 1 and 2 will not compile and elt the program run but statment 3 and 4 will behave exactly as like an normal and successful program is supposed to behave.

No comments:

Post a Comment