We help IT Professionals succeed at work.

What's the explanation for this not compiling ?

krakatoa
krakatoa asked
on
80 Views
Last Modified: 2018-10-10
What's the explanation for this not compiling ?

SSLSocketFactory factory = SSLSocketFactory.getDefault();

from, Java in a Nutshell, Flanagan, 5th edition, page 948.

?

Error reads :

: error: incompatible types: SocketFactory cannot be converted
o SSLSocketFactory
Comment
Watch Question

DarrenProject Manager / Technical Lead
CERTIFIED EXPERT

Commented:
Hi,

The class is a SSLSocketFactory is static so...

SocketFactory factory = SSLSocketFactory.getDefault();

Open in new window


Thanks,

Darren
DarrenProject Manager / Technical Lead
CERTIFIED EXPERT

Commented:
Or maybe....


SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

Open in new window

CERTIFIED EXPERT

Author

Commented:
I know it’s static.
The question is why it doesn’t compile.
CERTIFIED EXPERT
Top Expert 2016

Commented:
You can't assign a supertype to a subtype without casting. getDefault returns SocketFactory which is the supertype of SSLSocketFactory
Jan LouwerensEngineering Manager
CERTIFIED EXPERT

Commented:
This is implied in the comments above, but just to make it explicit:

SSLSocketFactory.getDefault() has a return type of SocketFactory. You cannot directly assign an object of type  SocketFactory to a SSLSocketFactory variable without casting.
CERTIFIED EXPERT
Top Expert 2016

Commented:
This is implied in the comments above
No - it's stated
CERTIFIED EXPERT

Author

Commented:
OK, I understand that, and in fact 'my' code has been all along :

SocketFactory ssl_s_f = (SSLSocketFactory)SSLSocketFactory.getDefault();

But what is the reason the book has it down as posted in my first comment?The context on the page  is this :

"Clients that want to perform simple SSL-enabled networking can create an SSLSocket with code like the following :
SSLSocketFactory factory = SSLSocketFactory.getDefault();
SSLSocket securesock = (SSLSocket)factory.getSocket(hostname, 443); //https port "

As an aside, I can't trace where the method 'getSocket(host,port)' originates from either. Any ideas ?
Jan LouwerensEngineering Manager
CERTIFIED EXPERT

Commented:

This is implied in the comments above
No - it's stated

You are correct. it was stated explicitly. Not sure how I missed that initially. My apologies.
CERTIFIED EXPERT
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
It's a mammoth blunder by the author imo. If he's referring to (an)other API he certainly isn't sharing the fact with his readers.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Wonder if the text is online. Can you quote something googleable from it?
CERTIFIED EXPERT

Author

Commented:
Nutshell SSL
CERTIFIED EXPERT
Top Expert 2016

Commented:
:)
I tried to check errata to no avail
CERTIFIED EXPERT

Author

Commented:
Right. The read-only language of print strikes again. ;)