Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with {}

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing else keyword so the other block is executed regardless of hasParams is true or false.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
Ketan Borkar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 4549
8
Java Netbeans IDE Scala
  • 3
  • Mark post as helpful
  • send pies
  • 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.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Linux Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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 {}.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic