wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem with {} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with {}" Watch "Problem with {}" New topic
Author

Problem with {}

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
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.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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


The quieter you are, the more you are able to hear.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4426
    
    8

The other problem is that when there are no parameters the args array will be empty. But it will not be null.
Ketan Borkar
Greenhorn

Joined: Oct 16, 2011
Posts: 2
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

Joined: Aug 31, 2012
Posts: 127
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

Joined: Aug 31, 2012
Posts: 127
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

Joined: Apr 06, 2010
Posts: 4426
    
    8

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.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216


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.


OCPJP 6 86%
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
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

Joined: Apr 21, 2012
Posts: 216

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 {}.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with {}