Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why would this code run without an error?" Watch "why would this code run without an error?" New topic

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

Joined: Jul 08, 2003
Posts: 24183

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]
It is sorta covered in the JavaRanch Style Guide.
subject: why would this code run without an error?
Similar Threads
simple instantiation from Mughal book
Another question from Khalid
KAM India Edition Question 6.27
Help needed on Inheritance
looking at this code, why Planet class is not a Star class?