Hello,
This is my first post Just getting into learning programming and I am starting with Java.
I'm currently reading the book Teach Yourself Java in 21 days and I'm learning about List, Logic and Loops.
There is a section about Block Statements in this chapter that states that The scope of a variable defined in a block is the block that it's in. So the variable can be used only within that block.
Then it has me write a program which seems to contradict that statement.
class DayCounter { public static void main(String args[]) { int yearIn = 2012; int monthIn = 1; if (args.length > 0) monthIn = Integer.parseInt(args[0]); if (args.length > 1) yearIn = Integer.parseInt(args[1]); System.out.println(monthIn + "/" + yearIn + " has " + countDays(monthIn, yearIn) + " days."); } static int countDays(int month, int year) { int count = -1; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: count = 31; break; case 4: case 6: case 9: case 11: count = 30; break; case 2: if (year % 4 == 0) count = 29; else count = 28; if ((year % 100 == 0 ) & (year % 400 != 0)) count = 28; } return count; } }
Apologies about the long block of code. To me it looks like it's taking the count variable from the 2nd block and importing it into the 1st block.
Although I suppose it is calling the entire method and not the specific count variable. Is that the case? or what am I missing here?