• Post Reply Bookmark Topic Watch Topic
  • New Topic

invoking method by refrence  RSS feed

 
saied ims
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Over{
void printHi(){
System.out.println("hi iam over");
}
void printHi2(){
System.out.println("hi iam testing2");

}
}
public class TestOver extends Over{


Over hh ;
public static void main(String [] islam){

Over r=new Over();
TestOver f=new TestOver();

r.printHi();
f.printHi();
}
void printHi(){

hh.printHi2();\\\ why this in not legeal its compile but runtime error , i used the hh refrence to invoke the method,why should i make a new Over hh=new Over(); thanks

}



}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you get a NullPointerException?

Instance variables that are object references that aren't initialized when the instance is created are initialized to null.

You can't call a method on a null reference.
[ July 10, 2006: Message edited by: Keith Lynn ]
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only call static methords with references , otherwise you will face NullPointerException if it is Instance Variable and if it is local variable then it will give compile time error , local variable may be initialzed.

Hope this helps you
Thanks
Regards
gaurav
 
saied ims
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its really help thanks all
 
saied ims
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Horse extends Animal {
private Halter myHalter;
public void tie(LeadRope rope) {
myHalter.tie(rope); // Delegate tie behavior to the
// Halter object
}
}
public class Halter {
public void tie(LeadRope aRope) {
// Do the actual tie work here
}
}

why here i didnt make a new(Halter myhalter=new Halter()
 
Buhi Mume
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myHalter variable must also be initialized.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference variable hh is used in static main method but it is not declared as static. This will give a compile time error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!