• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Problem with {}

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the following code print:
has params
no params



It's clear that it will print has params.But why no params?
Isn't the block
{


}

executed only when an object is created?And in this case there's no object created.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are missing else keyword so the other block is executed regardless of hasParams is true or false.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other problem is that when there are no parameters the args array will be empty. But it will not be null.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since args array is not null it is printing has params. but it does not contain any arguments.

if you want to check whether there are elements in the array or not you can use length as below:

[Added code tags - see UseCodeTags for details]
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kemal Sokolovic wrote:You are missing else keyword so the other block is executed regardless of hasParams is true or false.


yes i know that if i don't use else the next line would be printed.But in this case system.out.println is enclosed in { } block.
So, as far as i know { }blocks are called when an object of the class is created.But in my example no object is created so why does {} block execute?
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:The other problem is that when there are no parameters the args array will be empty. But it will not be null.


Yes, i know this, that's why it will print "has params".I want to know why is {} executed when no object is created.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Deshmkh wrote:So, as far as i know { }blocks are called when an object of the class is created.But in my example no object is created so why does {} block execute?


No, that's not true. Blocks using { } directly in the class body are initialiser blocks and are run on put object creation. But you've put the block in an ordinary method. A block like that will execute as usual - the only effect of the { } is to limit the scope of any variables declared within it.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It's clear that it will print has params.But why no params?
Isn't the block
{


}

executed only when an object is created?And in this case there's no object created.



Yes and no. its a block but its in a method so can't be a Initialization block which executes everytime when an object is created.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks guys, i understood.
{} is executed if it is present in an method no matter an object is created or not.
And if {} is present outside any method, then it is executed when an object is created.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Deshmkh wrote:
And if {} is present outside any method, then it is executed when an object is created.



Yup, but make sure that {} is alone not static {}.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic