This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Gloal Variables in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Gloal Variables in Java

Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
I want to declare a global variable in Java, and hence in the code below....

i use:

static int port = 10013 ; // Which is fine and dandy,
but i want to use this variable "port" as a global variable AND also as arg[0] so a user who runs the program can choose port on
command line.

I was able to do it, if i define:

int port = 0; //port number for Socket Connection, This will be arg[0]
if (args.length > 0) {
port = Integer.parseInt(args[0]);

But, then the variable "port" is NOT available in the run() method.

How can i make it as an arg[0] , and also have it available in the run() method

Thanks,

-Kamal.
}

import java.net.ServerSocket;

//Enhancement: Now instead of going to stdout , the output from the following lines should go to a Socket
//The ServerSocket represents a listening TCP connection
public class Server implements Runnable {
int id;

static int port = 10013 ;

public static void main(String[] args)
throws java.lang.InterruptedException {

//ServerSocket serverSocket = null;

//int port = 0; //port number for Socket Connection, This will be arg[0]
//if (args.length > 0) {
//port = Integer.parseInt(args[0]);
//}
System.out.println("Socket Port = " + port);
//if (args.length > 1) {
//fileName = args[1];
//}

for (int i = 0; i < 5; i++) {
Server runnable = new Server();
runnable.setId(i);
Thread thread = new Thread(runnable);
thread.start();
}

}
public void run() {

//Socket server = null;
ServerSocket serverSocket = null;
while (true) {

int num = NumberServer.getInstance().getNextNumber();

try {
//PrintStream out = new PrintStream(server.getOutputStream());
serverSocket = new ServerSocket(port);
System.out.println("before " + num);
//out.println(num);
System.out.println("after " + num);

} catch (Exception e) {
System.out.println("PrintStream: " + e);
//e.printStackTrace();
}

}

//System.out.println(
//"id: "
//+ this.id
//+ ": "
//+ "Number Server's instance Number : "
//+ num);

//try {
//Thread.sleep(1000);
//} catch (InterruptedException e) {
//
//e.printStackTrace();
//}

}

public void setId(int i) {
id = i;
}

}
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

Kamal,
You have two options:
1) Use a different name for the local variable port
2) Reference port as Server.port in the run method

I prefer option one as it is less confusing.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Thanks, Jeanne .

What i want ot do is:

1. Create a ServerSocket on a port
2. Create a Socket
3. Send the data :
//System.out.println(
//"id: "
//+ this.id
//+ ": "
//+ "Number Server's instance Number : "
//+ num);

over this socket:
Here is the code without sockets, could you give me some hint on how to do this. I HAVE ordered a book on Java Socket Programming, but in the meantime...

Thanks in Advance.

public class Server implements Runnable {
int id;
public static void main(String[] args)
throws java.lang.InterruptedException {
int port = 0; //port number for Socket Connection, This will be arg[0]
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
System.out.println("Socket Port = " + port);

for (int i = 0; i < 5; i++) {
Server runnable = new Server();
runnable.setId(i);
Thread thread = new Thread(runnable);
thread.start();
}

}
public void run() {
while (true) {
int num = NumberServer.getInstance().getNextNumber();
try {
} catch (Exception e) {
System.out.println("PrintStream: " + e);
//e.printStackTrace();
}
}
//System.out.println(
//"id: "
//+ this.id
//+ ": "
//+ "Number Server's instance Number : "
//+ num);

//try {
//Thread.sleep(1000);
//} catch (InterruptedException e) {
//
//e.printStackTrace();
//}

}
public void setId(int i) {
id = i;
}
}


-Kamal.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
See my reply in your duplicate post (tisk tisk ) in the Sockets forum -- moved to this forum.
[ February 27, 2005: Message edited by: David Harkness ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gloal Variables in Java