1

I have some code that I am working on. It's basically takes in user input and creates a directed graph. One person can travel one way, the other person the opposite. The output is the overlap of where they can visit.

I have most everything working the way that I want it to, but I am concerned with the use of static that I have. I don't seem to fully understand it and no matter where I look, I can't find out its exact use OR how to get rid of it.

Could someone please help me to understand what static is and why it would be helpful?

Also, would it be better to move most the code from MAIN to helper methods? If I do this I have to move all my variables from main to the top of the class and then they all have to be declared as static?!

  • My wife says _static guard does just fine_ :)... but to answer your question, static makes the method available at the Class level, not the Instance level. Static methods can only access static variables as their is no _instance_ at that level. Anyway, I think you might benefit from this: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Lucas Apr 11 '13 at 23:16
  • If this is small scale, wouldn't it be simpler to have this at instance level or are there downsides to that –  Apr 11 '13 at 23:19
  • main methods are typically minimal. You would most likely just create an instance of something and tell it to do something. But from the code I see, I assume you are either doing this for homework or new to java, so that may dictate your approach... – Lucas Apr 11 '13 at 23:22
  • I'm new to java you could say. I'm trying to learn this on the side along with everything else I am doing. One of my big concerns right now with this example is making it run efficiently on possibly a large scale –  Apr 11 '13 at 23:26
  • you should look into some java beginner tutorials first. There are some fundamentals that you should get ironed out before worrying about efficiency and scale. (I intend this as constructive, so I hope you don't take offense). – Lucas Apr 11 '13 at 23:29

4 Answers4

3

The reason everything has to be static is because you aren't creating any objects. If you were to create an object by calling new in your main method, you could use non-static variables on that object. This isn't really a good place to give you a tutorial on why you might want to use object-oriented design; you can find one of those online to read (a commenter above gave a possible reference). But the reason everything has to be static is because it's all just running from the main method, which is always static in java. If you were to call new somewhere, you could use non-static variables.

mattg
  • 1,731
  • 1
  • 12
  • 20
1

Static makes a method or a variable accessible to all the instances of a class. It's like a constant, but for classes. To make it more easy to understand some code will do the work:

public class Example {
  public static int numero;
}

public class Implementation {

  public static void main (String args[]) {
    Example ex1 = new Example();
    Example ex2 = new Example();
    Example.numero=10;
    System.out.println("Value for instance 1 is: " + ex1.numero);
    System.out.println("Value for instance 2 is: " + ex2.numero);
  }
}

Running the follwing code will output:

Value for instance 1 is: 10 Value for instance 2 is: 10

Because you set the static variable numero (number in italian) to 10.

Got it?

Liquid Core
  • 1
  • 6
  • 27
  • 52
  • So this would imply that the variable is stored at a specific place in memory no matter how many instances are created correct? –  Apr 11 '13 at 23:27
  • Yeah, something like that. You set it once and every instance of that object will use it. Anyway someone more expert may confute what I'm saying, but atm, I'd say you're right. Also, I'm not very expert (like you, if I figured out right) so I always used static variables when public modifies wasn't enough and was giving me weird access errors. After that those errors vanished. Just saying to give you a real life example of how I used static. – Liquid Core Apr 11 '13 at 23:40
  • Check this one out too http://stackoverflow.com/questions/3963983/how-and-where-to-use-static-modifier-in-java – Liquid Core Apr 11 '13 at 23:44
  • Glad to help. Good coding! – Liquid Core Apr 11 '13 at 23:52
1

It looks like a lot of your static methods (findNodeInList, etc) all take the ArrayList (which represents a map) as their first argument. So instead of having it static, you could have a class Map, which stores a list of nodes and has methods on them. Then the main method would read the input, but not have to manage any nodes directly. e.g:

class Map {
  ArrayList<Node> nodes;
  public void addNode(Node n) { nodes.add(n); }
  public int findNodeInList(String s) { ... }
  ...
  public static void main(String[] args) {
    Map peggyMap = new Map();
    Map samMap = new Map();
    // Read the data
    samMap.add(new Node(...));
  }
}

This keeps all the stuff to do with nodes/maps well encapsulated and not mixed in with stuff to do with reading the data.

chrisd
  • 21
  • 3
0

Static is useful if you going to be using the class/method throught out your program and you don't what to create a instance every time you need to use that method.

For ex

public class StaticExample {

  public static void reusable() {
  //code here
  }
}

It means you can use it like this

StaticExample.reusable();

and you don't have to create an instance like this

StaticExample staticExample = new StaticExample();

staticExample.reuseable();

I hope this help you decide whether to use static or not.

AGDEV
  • 82
  • 6