I'm afraid you're misinterpreting what the author is saying:
int hour = 11;
int minute = 59;
printTime(int hour, int minute); // WRONG!
In Java, you do have to specify the type of each variable you use, but you only do so once - when you declare it initially, which is what is being done in the first two lines of the code above.
int hour = 11;
int minute = 59;
printTime(hour, minute); // RIGHT!
Thereafter, whenever Java encounters one of these variables, it looks back to its initial declaration to determine its type. This is done automatically: You do not have to re-specify a variable's type each time you use it - and indeed, as you've discovered, you cannot, as doing so results in a syntactical error that will prevent your code from compiling.
(Note that this paradigm holds true in all strongly typed languages, of which Java is one of many).
I wonder if you're confused after having seen things like this
void printTime(int hour, int minute){
}
which is correct. The key difference, of course, is that here we are looking at a method declaration: In this context, (int hour, int minute)
is not analogous to the third line of the code above but rather to the first two lines: It is declaring two variables of type int
named hour
and minute
in the context of that method.
What's different is that, in the code above, the variables are being declared and initialized in one step, but note that:
int hour; //variables DECLARED (given a name and type)
int minute;
hour = 11; //variables INITIALIZED (set to an initial value)
minute = 59;
is valid as well, and this is essentially what's happening in our method declaration: (int hour, int minute)
declares two int
variables named hour
and minute
, but they don't get initialized until the method is called, at which point they're set to the values passed in as parameters.
Update:
That being said, I'm merely speculating here about the source of your confusion, which is ultimately about how to invoke methods rather than with how to declare them.
printTime(int hour, int minute); // WRONG!
printTime(hour, minute); // RIGHT!
In your comment, and in your update, you referred to what is being done here as initializing a method, which I presume you're imagining as being similar to the way you initialize a variable - and I wonder if this might be at the root of your confusion:
You're not initializing the printTime( )
method here, you're invoking it - you're calling it. Methods do not get "initialized" in Java the way variables do, they get declared (see above) and then they get used, which is what we see happening here.
You're encouraged to use verbs for method names because methods do things - for instance, printTime
(presumably) prints the time. Invoking a method is saying do that thing: You're not describing that thing, the way you describe a variable when you initialize it - that's what you do in your method declaration). When you invoke a method - when you call printTime()
- you're saying do the thing - print the time. You're not initializing the method, you're using it.
A method invocation is signified by a set of parentheses at the end of it. Sometimes these parentheses are empty: For instance, one might expect a printTime()
method to print the current time. Other times, we want methods to do things with things, and we give it those things by passing them in as parameters: printTime(int hour, int minute)
, will presumably print whatever time is signified by the hour
and minute
parameters passed into it: printTime(11,59)
should print 11:59
.
int hour = 11;
int minute = 59;
printTime(hour, minute);
would do the same. Passing them in as parameters to methods is one of the many ways we use variables in Java. In order to use one in any of those ways, it must first be declared and given a type. Thereafter, it can be used in any of those ways without reiterating that type. Indeed, it must, because doing so will result in a syntax error every time.
(PS, I realize I veered a bit from answering your original question, but you inadvertently asked me another in your comment, one that I think is important)