Link to home
Start Free TrialLog in
Avatar of albertlee
albertlee

asked on

JDK Compiler error with Socket class

I wrote a simple program as below,

import java.io.*;
import java.net.*;

class MyMain
{
  Socket s;

  public static void main()
  {
     s = new Socket("263.net", 110);
     ....

But when compiling, the compiler displayed an error "Can not make a static reference to a non_static xxxx
(maybe variable, class, or something like that):
  s = new Socket("263.net", 110);  "
  ^

I'm completely puzzled and I have typed everything exactly as  printed in the book. I myself can not see anything wrong in the code. Could you please offer an answer? and by the way, I'm trying to write an applet dealing with POP3 and SMTP server. If you can give me one (doesn't need to be a complete applet, some explanation about the POP3 and SMTP server can also do), I'll be greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jod
Jod

Maybe I will briefly I will anyway.

The importance is in knowing the difference between a class and an instance of a class.

Variables and methods in Java can have one of two states. They can be either Static or non static.

To make a variable static means it is always available for use whether or not you have created an instance of the class.

So to make an instance of the class you do this:

String s = new String ("Instance of a string");

this creates a new instance of a string and places it in the object s.

The reason you could not put the string into s in your code is that you were trying to do it directly in the static method main of your class MyMain.

What's a static method?

It is one that can be used WITHOUT having to create an instance of the class. It's like saying:

String.dosomething("do it with this");

but you do not have to create a new string s to do it. You are allowed to because it makes sense to not need to create an instance of a class to do certain things.

And it just so happens that to run the main method of a class is one of these occasions.

For each MyMain class there can be only one static main method shared between all instances in your code above - that is what you are saying when you make main static. However, there is a new s allocated every time you make a new MyMain object.

You run the main method of your class by compiling your class:

javac MyMain.java

and then run it using:

java MyMain

Java knows what to do with your class because it finds the method main which is always there and there is only one. If there was no method called main or if main was not static it would not work because the main function would not exist until you created a new MyMain object. But you can't do this until you are in the code - but you can't get into the code and so on, and on...

So main is a boot strap method to jump start Java into life - it is a special case of a static method. But static methods are quite common where it does not make sense to have a new version of the method for each instance of the class - you just need one version that is the same for every version.

Try it, by changing the name of the method main to main1 and make it non static

class MyMain
{
  Socket s;

  public void main1()
  {
     MyMain m = new MyMain();
     m.s = new Socket("263.net", 110);

now it will compile, but you will find that when you try to run the code it will complain the MyMain has no method main it can find to run it. Without main it doesn't know where to start.

The important point is, that a static method should only refer directly to static variables. s in your code is not static so it does not exist until you do:

MyMain m = new MyMain();

whereas the main method is ALWAYS there. Thuis it cannot find an s to refer to yet and it wouldn't know which s to refer to if you had made more than one socket s.

Full and better explanation follows, from thinking in Java:

Having problems posting the message... but it is here:

http://www.codeguru.com/java/tij/tij0037.shtml#Heading79
one more sample :) (with local variable)

 public static void main()
  {
     Socket s = new Socket("263.net", 110);
  }
Yep, another possible version.

albertlee, are you OK with this now?

heyhey_,

I think I may have scared him off with a rather over long answer...

Avatar of albertlee

ASKER

Thank you very much, Jod,
I think it is a minor mistake, but it caused me much trouble.
Well, in a OOP language, you have to consider every aspect of things but I'm a novice.
Thank you again, Jod.