[5943] in java-interest

home help back first fref pref prev next nref lref last post

Re: Static variable assigment

daemon@ATHENA.MIT.EDU (Thomas Ball)
Tue Mar 5 19:46:28 1996

Date: Fri, 1 Mar 1996 06:52:03 -0800
From: Thomas.Ball@Eng.Sun.COM (Thomas Ball)
To: java-interest@java.Eng.Sun.COM

Your server_port variable doesn't have a static modifier.  With Java,
the static modifier makes a class variable or method; without it, the
variable or method is always associated with a specific class instance.

In your example, the main() method is static, so (being a class method)
it can only access methods and variables which are also static.  You
have two options:

1.  Make server_port static; or
2.  Create an instance of class Connect, which can access server_port.  

Tom Ball
Java Products Group

>  May be I am asking silly question but I bit suporised that very simply
>  peace of code cause troubles for me. I wrote applet
>  where I would like to assign integer:
>  class Connect extends Applet
>  {
>                   private final static int DEFAULT_PORT=4321;
>                   private int server_port=DEFAULT_PORT;
>      
>          public static void main(String args[])
>          {
>  
>                  try
>                  {  
>                          server_port = (int)Integer.parseInt(args[0]);
>                          // <<<< Error is here (line 67) !!!!
>                  }
>                  catch(Exception e)
>                  {
>                     System.out.println("Defaulting to port " + DEFAULT_PORT);                                             
>                  }
>                 
>                  new Connect();
>  
>  
>  .........ommited........
>  
>  When I compiling this the java respond to me:
>  
>  javac Gonnect.java
>  Connect.java:67: Can't make a static reference to nonstatic variable server_port in class GenericServer.
>                          server_port = (int)Integer.parseInt(args[0]);
>  
>  Could anybody to say what does it mean?

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post