Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sridevi kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding your first example, I'm not sure why you are experiencing problems. It works fine for me.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding your second example, I'm not sure why you are having problems. It works fine for me.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, thank u very much dirk.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic