This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design 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: 24190

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: why would this code run without an error?
It's not a secret anymore!