Avatar of krakatoa
krakatoa
Flag for United Kingdom of Great Britain and Northern Ireland asked on

What's the explanation for this not compiling ?

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
Java

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
Darren

Hi,

The class is a SSLSocketFactory is static so...

SocketFactory factory = SSLSocketFactory.getDefault();

Open in new window


Thanks,

Darren
Darren

Or maybe....


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

Open in new window

krakatoa

ASKER
I know it’s static.
The question is why it doesn’t compile.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CEHJ

You can't assign a supertype to a subtype without casting. getDefault returns SocketFactory which is the supertype of SSLSocketFactory
Jan Louwerens

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.
CEHJ

This is implied in the comments above
No - it's stated
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
krakatoa

ASKER
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 Louwerens


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.
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
krakatoa

ASKER
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
CEHJ

Wonder if the text is online. Can you quote something googleable from it?
krakatoa

ASKER
Nutshell SSL
CEHJ

:)
I tried to check errata to no avail
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
krakatoa

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