I'm just starting to learn java, and our teacher gave us a code to analyze and explain to him on the next lecture. I understand most of it, however I have a problem with 1 line of code.
public class Test {
public static void main(String[] args) {
PascalTriangle WTP;
try {
WTP = new PascalTriangle(Integer.parseInt(args[0]));
} catch (NegativeArraySizeException ex) {
System.out.println(args[0] + "\t- incorrect data");
return;
} catch (NumberFormatException ex) {
System.out.println(args[0] + "\t- incorrect data");
return;
}
for (int i = 1; i < args.length; i++) {
try {
System.out.println(args[i] + "\t-> " + WTP.wspolczynnik(Integer.parseInt(args[i])));
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println(args[i] + "\t- incorrect number");
} catch (NumberFormatException ex) {
System.out.println(args[i] + "\t- incorrect data");
}
}
}
}
The rest of the code isn't important in my question, so I skipped it (this program is about pascal triangle). I don't understand this line:
PascalTriangle WTP;
PascalTriangle is the name of the other class, responsible for all the operations in this program. Could you explain to me what happens in this line of code? Is this some kind of "shortcut" , so we can just write WTP instead of typing PascalTriangle every time? I apologise if my question looks silly, but I couldn't find an answer on my own, thanks for the help.
2 Answers 2
Notice where you say int i = 1. This is actually a shorthand for two different things: variable declaration and initialisation. You could also write that in two parts:
int i; // declaration
i = 1; // initialisation
The declaration part tells the compiler that you intend for i to represent an integer variable. The initialisation expression sets its value to 1.
In the same way, you have both parts with respect to the variable WTP:
PascalTriangle WTP; // declaration
WTP = new PascalTriangle(Integer.parseInt(args[0])); // initialisation
The first of these two lines says you intend to have a variable called WTP in this function, and that its type will be PascalTriangle. Then, you create a new PascalTriangle and assign it to the variable.
WTP is in no way a shortcut for PascalTriangle. PascalTriangle is of type Class; WTP is of type PascalTriangle. Or, simply speaking, PascalTriangle is a class, and WTP is an instance of that class. Think of the difference between "doghood" and "my dog Fido". "My dog Fido" might have four legs. "Doghood" doesn't have legs; or if we did count, it would have millions upon millions. But "my dog Fido" is a fine example of "doghood", if that makes sense. In the same way, the thing that we assign to WTP is an example ("an instance") of PascalTriangle; you could have other PascalTriangles, just like I could have another dog. When you write WTP.wspolczynnik, this is referencing an instance method; if you wrote PascalTriangle.wspolczynnik, that would only work if the method was declared to be a class method (using the static keyword), like "doghood" having population but "dog" having legs.
Comments
Is this some kind of "shortcut" , so we can just write WTP instead of typing PascalTriangle every time?
Sort of? But that's not really how you should think about it. Instead, consider the difference between a class and an object. PascalTriangle is a class, WTP is an object. An object is an instant of a class.
In this case, that line of code is declaring a variable called WTP of type PascalTriangle.
As an analogy, think of a class as the blueprints for a house. Think of an object as an actual house. The act of building a house from the blueprints is like calling the class' constructor. It perhaps takes in a few options, and produces an instance of the house based on the blueprints.
In this case, the constructor wasn't actually called. That would look like this:
PascalTriangle WTP = new PascalTriangle();
So, to continue the analogy, in your line of code the house hasn't actually been built yet. But the specific blueprints (class type) have been selected and the lot (memory address) has been reserved. In a later line:
WTP = new PascalTriangle(Integer.parseInt(args[0]));
The house is constructed on that lot.
[object type] [variable name].