Java Code Question (Method Calling)

Hi, I’m trying to learn java, and this is the first text-based programming language that I’ve gotten serious about. I am fairly proficient with labVIEW, so a lot of the syntax is new and kind of strange for me, but I digress. I have a particular question involving the following code. Also, if anyone could point me in the direction of any good learning resources for FRC java programming, that would be great. Thanks in advance!

package practicePackage;

class Planets {
	int moons;
	String name;
	
	Planets (String name, int moons) {
		this.name = name;
		this.moons = moons;
	}
	
	void display() {
		System.out.println(name + " has " + moons + " moons.");
	}
}

class SolarSystem {
	Planets planets];
	
	SolarSystem() {
	
	planets = new Planets[9];
	planets[0] = new Planets("Mercury", 0);
	planets[1] = new Planets("Venus", 0);
	planets[2] = new Planets("Earth", 1);
	planets[3] = new Planets("Mars", 2);
	planets[4] = new Planets("Jupiter", 16);
	planets[5] = new Planets("Saturn", 18);
	planets[6] = new Planets("Uranus", 15);
	planets[7] = new Planets("Neptune", 8);
	planets[8] = new Planets("Pluto", 1);
	}
	void display() {
		for (int i = 0; i < planets.length; i++ ) {
			planets*.display();
		}
	}
}

class MilkyWay {
	public static void main(String args]) {
		SolarSystem solarSystem = new SolarSystem();
		solarSystem.display();
	}
}

Why/how is it that the display method in the SolarSystem class calls the display method in the Planets class? Why is it that it doesn’t call in itself recursively?

If it helps, this is the output of the program.

Mercury has 0 moons.
Venus has 0 moons.
Earth has 1 moons.
Mars has 2 moons.
Jupiter has 16 moons.
Saturn has 18 moons.
Uranus has 15 moons.
Neptune has 8 moons.
Pluto has 1 moons.*

If I understand your question correctly - planets* references a Planet object, so it’s going to call the display() method in that class.*

This site is pretty helpful for learning and practicing Java in general. After you get the hang of Java, go here for FRC-specific Java information and samples.

@dcarr, Thanks a bunch. That seems to make a lot of sense now.

@markmcgary, thanks for the resources.

To expand further, to call SolarSystem’s display method inside the SolarSystem class, you’d use just “display()” or “this.display()”.

“this” is a reserved word that is a reference to the current object that the code is running in/from, and is usually implied, but there are cases where you’d want to use it (like a method having arguments named the same thing as class variables)