Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Block Statements Question

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Block Statements Question

    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?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Block Statements Question

    taking the count variable from the 2nd block and importing it into the 1st block.
    Not sure I see what you are asking about. The count variable is only known with the {}s surrounding the code in the countDays method.
    The contents of the count variable is returned by the countDays method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Block Statements Question

    Quote Originally Posted by Norm View Post
    Not sure I see what you are asking about. The count variable is only known with the {}s surrounding the code in the countDays method.
    The contents of the count variable is returned by the countDays method.
    I guess in the first block the countDays from the second block is called. So that is kosher?

    If there was a line in the second block that said:

     int x = 3'

    That would not be able to be accessed from the first block but methods are ok to call?

    I guess I don't understand what information can be passed between blocks. Because the above program is passing some information between the two blocks.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Block Statements Question

    Methods are defined within the class's enclosing {}s. Since all the methods are in the DayCounter class's enclosing {}s then code in any method in the class can call any other method in the class.

    Methods can be declared to return A VALUE. Only one value can be returned. The calling code then should assign the returned value to a variable or used it in some way.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Nitnatsnok (July 14th, 2020)

Similar Threads

  1. Finally Block
    By AbbasAlasgarov in forum Exceptions
    Replies: 5
    Last Post: October 25th, 2017, 04:37 AM
  2. Questions about if statements and if-else statements
    By chakana101 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 23rd, 2014, 05:37 PM
  3. Replies: 10
    Last Post: December 22nd, 2013, 07:36 PM
  4. New Kid on the Block
    By charles.oseibonsu in forum Member Introductions
    Replies: 2
    Last Post: October 30th, 2013, 09:56 AM
  5. I have a question about import statements
    By syedk01 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 27th, 2013, 07:47 AM