Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Main method

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I am preparing for OCA and I am not able to understand a syntax that is used in the main method, please help me with this.



I am not able to understand what is the syntax on line 7 and how it is used, I am sure it is not Instance block, it is something else, please help me with this.
 
Marshal
Posts: 73738
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said before, please tell us where these questions come from, so wee can assess them. The answer given for your first question about arrays and ArrayLists was wrong.
No, that can't be an instance initialiser, because
  • 1: You are there in a static method, and,
  • 2: initialisers sit “naked” inside the class just as methods do.
  • No, it is just like end of line 4‑line 6, a plain simple block. As you know, the Java┬« Language Specification (=JLS) defines statement such that a block can stand in for a statement so the one statement after if (something) or else can be replaced by a block.
     
    sahil Kairon
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Campbell Ritchie for explaining this, Actually this is not any mock question for  OCA, I wrote this myself and I came across this syntax many times, so I was not aware of what it is.

    Thank you once again for explaining this and I will make sure in future that If I know the source of question, will definitely post it along the question.

    So as per your explanation JLS will treat this block as normal statement.
     
    sahil Kairon
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And the array and arraylist question that I posted before, I also felt that it is wrong, that is the reason I thought of sharing the question on this forum as I am a beginner, so that I can get clarification rather than developing wrong understanding.
    And the source of that question was:  -

    https://vceguide.com/identify-two-benefits-of-using-arraylist-over-array-in-software-development/

     
    Marshal
    Posts: 8050
    569
    Mac OS X VI Editor BSD Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @OP

    I've moved your question to other forum, a bit more appropriate for this kind of question.

    When you say you don't understand syntax, is there some particular line you don't understand, or all lines?

    Do you understand boolean logic as such?
     
    sahil Kairon
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Liutauras Vilda for the help. Actually I was not able to understand the code on line 7 which is
    {   System.out.println("World");    }

    Other than this everything is clear to me.
    I just wanted to know, how we can use block inside main method and what are its significance,
    do it act like normal code statement and curly braces do not effect it ?
     
    Liutauras Vilda
    Marshal
    Posts: 8050
    569
    Mac OS X VI Editor BSD Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sahil Kairon wrote:I just wanted to know, how we can use block inside main method and what are its significance,
    do it act like normal code statement and curly braces do not effect it ?


    Oh I see...

    Significance.. Try to compile this code and let's see what you can understand from it:
     
    sahil Kairon
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the reply, I have tried running the code and this code gives compilation error when we try to call int variable ('b' declared in the block) outside of the block.
    So I guess the scope of anything we write in this block is limited to the block itself. And it is treated as local variable.

    Please correct me if I am wrong.
     
    Liutauras Vilda
    Marshal
    Posts: 8050
    569
    Mac OS X VI Editor BSD Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sahil Kairon wrote:So I guess the scope of anything we write in this block is limited to the block itself.


    You are spot on! Well done.
     
    sahil Kairon
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you so much Liutauras Vilda. Really appreciate it.
     
    Goodbye moon men. Hello tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic