aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is it legal for an assertion to create a new Class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is it legal for an assertion to create a new Class?" Watch "Is it legal for an assertion to create a new Class?" New topic
Author

Is it legal for an assertion to create a new Class?

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Hi,

Assuming all other things are correct, will this cause the compilation for the code to fail?



If not, why not?

Thanks :-)


OCPJP 6, OCMJD
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
The expression after the ':' is used to print a string to the screen and must evaluate to some value. So doing

with

does not compile.

compiles fine. Running it with asertions enabled will give something like 'java.lang.AssertionError: A@fjdhs' depending on the Car toString() method.

John
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

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: 2408
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: 8815
    
    5
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.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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.
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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?

Thanks.
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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: 185
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

but you cannot do

with

John
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

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.
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

That makes sense, thanks Johan and Matthew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it legal for an assertion to create a new Class?