[5884] in java-interest
Re: Static variable assigment
daemon@ATHENA.MIT.EDU (Mike Paluka)
Fri Mar 1 20:40:42 1996
Date: Fri, 01 Mar 1996 16:10:13 -0800
To: vitaly@diogen.asc.rssi.ru (Vitaly Promislov)
From: Mike Paluka <mikep@exmachina.com>
Cc: java-interest@java.sun.com
At 02:58 PM 3/1/96, you wrote:
>Hello All
>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?
>
"main" is a static function, meaning that it doesn't have a 'this' pointer,
and therefore, can't access members of the class unless they are also
declared static, OR, you instantiate the class.
Change this:
private int server_port=DEFAULT_PORT;
To this:
private static int server_port=DEFAULT_PORT;
Or, create a class instance ("new Connnect()"), and access your local
variable thru the reference returned:
Connect myConn = new Connect();
myConn.server_port = ...
--
*************************
Michael J. Paluka
Senior Software Engineer
Ex Machina, Inc.
mikep@exmachina.com
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com