Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Global Variables in Java

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}

}
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note to bartenders: this is unrelated to Sockets and should be in the Java in General (Beginner) forum.

In this codesimply remove the first line (declaration of port) and uncomment the other three lines. If the user specifies a port on the command line, the if test will pass and the static port shared by all Server instances will be written over.

However, a better way (you generally want to avoid "global" variables like this) would be to add the port as an argument to Server's constructor (you'd have to add a constructor). This would allow you to have multiple Servers running on different ports. This requires
  • Change "static int port" to "int port" (instance field).
  • Create a constructor for Server that takes an "int port" and assigns it to the instance field.
  • Create a constructor that takes no arguments and assigns a default port to its instance field.
  • Parse args[0] if available and pass to Server constructor; otherwise call no-arg constructor.


  • This isn't mandatory to do what you want, but it is cleaner.
    [ February 27, 2005: Message edited by: David Harkness ]
     
    author and iconoclast
    Posts: 24204
    44
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by David Harkness:
    Note to bartenders: this is unrelated to Sockets and should be in the Java in General (Beginner) forum.



    Yep. And that's where I'm moving it.
     
    permaculture is largely about replacing oil with people. And one tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic