This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why would this code run without an error? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "why would this code run without an error?" Watch "why would this code run without an error?" New topic
Author

why would this code run without an error?

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
I don't understand why the code below would run without error since object orbiting has not been instantiated in the class Planet?

public interface HeavenlyBody { String describe(); }

class Star implements HeavenlyBody {
String starName;
public String describe() { return "star " + starName; }
}

class Planet {
String name;
Star orbiting;
public String describe() {
return "planet " + name + " orbiting " + orbiting.describe();
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

It's not a complete program, as there's no "main" routine. The program could certainly run without error if no one called Planet.describe().


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: why would this code run without an error?
 
Similar Threads
simple instantiation from Mughal book
Another question from Khalid
Help needed on Inheritance
KAM India Edition Question 6.27
looking at this code, why Planet class is not a Star class?