user1360461
user1360461

Reputation:

Is static bad? How to remove static variables?

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?!

Upvotes: 1

Views: 7676

Answers (4)

chrisd
chrisd

Reputation: 21

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.

Upvotes: 1

Liquid Core
Liquid Core

Reputation: 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?

Upvotes: 1

mattg
mattg

Reputation: 1855

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.

Upvotes: 3

AGDEV
AGDEV

Reputation: 82

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.

Upvotes: 0

Related Questions