This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to use the Socket() constructor in java.net.Socket? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use the Socket() constructor in java.net.Socket?" Watch "How to use the Socket() constructor in java.net.Socket?" New topic
Author

How to use the Socket() constructor in java.net.Socket?

sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
from the api:
protected Socket()
- Creates an unconnected socket, with the system-default type of SocketImpl.

1)
if i write a class as follows:
import java.net.*;
public class SocketDemo
{
private Socket m_objSocket; //line 1
public SocketDemo()
{
m_objSocket = new Socket(); //line 2
}
}
when i compile the above, i am getting the following error:
SocketDemo.java:31: No constructor matching Socket() found in class java.n
et.Socket.
m_objSocket = new Socket();
^
1 error

2)
if i write a class as follows:
import java.net.*;
public class SocketDemo extends java.net.Socket
{
protected Socket m_objSocket; //line 1

public SocketDemo()
{
m_objSocket = new Socket(); //line 2
}
}
when i compile the above, i am getting the following error:
SocketDemo.java:31: Can't access protected constructor of class SocketDemo. Instance creation is permitted only within the package in which the constructor is defined.
m_objSocket = new Socket();
^
1 error

Could someone help me how to use the constructor "new Socket()" in java.net.Socket?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Regarding your first example, I'm not sure why you are experiencing problems. It works fine for me.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Regarding your second example, I'm not sure why you are having problems. It works fine for me.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Ah ha! You must be using a JDK that is "pre 1.1"! You'll notice that the no argument constructor is "since 1.1". I checked the Java 1.0 documentation and the no argument constructor was initially protected but changed to public for the Java 1.1 release.
Making sense?
sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Hi Dirk:
Thanks a lot...
The problem is: I am using jdk 1.3...
Only from jdk1.4, that constructor is public...
I checked in 1.3 api. It is protected.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Yep. (Got my versions a lil' mixed up)
So, problem solved. Right? There is no way around this. Don't even think about it.
sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Once again, thank u very much dirk.
 
GeeCON Prague 2014
 
subject: How to use the Socket() constructor in java.net.Socket?