IntelliJ Java IDE
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
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 69

Hi,

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



If not, why not?

Thanks :-)


OCPJP 6
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 165
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: 2686

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

Joined: Jul 13, 2011
Posts: 69

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

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

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

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

That makes sense, thanks Johan and Matthew
 
IntelliJ Java IDE
 
subject: Is it legal for an assertion to create a new Class?
 
Threads others viewed
Recursive number problem
question on iteration
array[] [] ?
negative integer
x-- -x/0??
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture