And just to be picky (you'll find that's quite common round here, but then, programming is all about attention to detail ), you aren't creating a new class there. You're creating a new instance of a class.
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
0
I'm perennially bemused by this kind of question that can be answered with the help of a compiler a LOT quicker than posting here and waiting for an answer.
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8439
posted
0
In the case of assertions it's important to ask two questions:
- Is it legal? - which of course can be answered by the complier - Is it appropriate, according to Sub / Oracle standards?
The "appropriate" question might be on the real exam.
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Tim Moores wrote:I'm perennially bemused by this kind of question that can be answered with the help of a compiler a LOT quicker than posting here and waiting for an answer.
Good lad Tim. Sorry to hear about your bemusement. Its just that the compiler cannot tell me if there is an error in the book or not. In order to cure you bemusement, I suggest you do a Google image search for lolcats.
Hi John, thanks for your reply. I suppose I should have posted the question in full instead of making my own variation...
The book states that compilation fails due to "assert(j==12):doStuff();" as it does not return a value. The book does not indicate that they're is anything wrong with the line after which is "assert(j==12):new Clumsy();".
As far as I can tell, "assert(j==12):new Clumsy();" will not return a value either. Why does the compiler not complain about this line?
Matthew Brown wrote:And just to be picky (you'll find that's quite common round here, but then, programming is all about attention to detail ), you aren't creating a new class there. You're creating a new instance of a class.
thanks Matthew, I was wondering how to phrase the question when I initially typed it ;-)
John Stark
Ranch Hand
Joined: Jul 19, 2011
Posts: 165
posted
1
As far as I can tell, "assert(j==12):new Clumsy();" will not return a value either. Why does the compiler not complain about this line?
Hm, yes. The compiler wants something that gives a string which can be printed out together with the assertion error. So I guess the toString() method of the created Clumsy object is called somehow. Like in System.out.println() where you can do
Glen Iris wrote:As far as I can tell, "assert(j==12):new Clumsy();" will not return a value either. Why does the compiler not complain about this line?
It does have a value - the new Clumsy object. It's not a String, but that's not important - it will accept any value there. John's example demonstrates the difference quite nicely.