Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why would this code run without an error?

 
sura watthana
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic