Link to home
Start Free TrialLog in
Avatar of jdav3579
jdav3579

asked on

value in first position of one array overwritten by value in second array

Hi,
I have the following code(below), I want to create two arrays which simulate roads. The "road" has a particular type of car at position [0] in one road and another type of car in position[0] in the other road. There is no connection between the two roads apart from they are arrays of car objects. Unfortunatley, when I populate the array, the first roads firat car is overwritten by the type of car in the second array and I just cant understand why.
I am new to java, so am just trying to get my head round it.
Any help greatly appreciated
Cheers
John

import java.util.*;

public class car
      {
      public static String name = new String();
      public Ship(String Carname)
            {
            name= Carname;       
            }
      }

import java.util.*;

public class CarSim
      {
      public static void main(String[] args)
            {
                  Car[] Road1 = new Car[100];
                  Car[] Road2 = new Car[100];
                  Road1[0].name="Fiat";
                  Road2[0].name="Renault";
                  System.out.println(Road1[0].name);
                  System.out.println(Road2[0].name);
            }
      }

Avatar of Ajay-Singh
Ajay-Singh

Post the full code, the code you have shown would throw a NullPointerException "Road1[0].name="Fiat";" here
> public static String name = new String();

I think this field should be non-static. static fields are same on all the instances of the class. So, change that to

public String name = new String();
Avatar of jdav3579

ASKER

Hi Ajay-Singh,
Thanks for your comments.
This is the full code, obviously over 2 files, but thats the whole thing. It doesnt throw any exceptions though. It just shows renault for both arrays at position zero where one array should have fiat!
I am also not sure what you mean about the non-static field and how that works?
Cheers
John
ASKER CERTIFIED SOLUTION
Avatar of ADSLMark
ADSLMark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Static vs non-static

Let me tell you a very basic story on how you can look at this.
You have several types of containers, for example a square, rectangle, sphere, etc. Now assume that we can create these containers. We only have to specify what type of container we want to create. So if i want a square container, then i say: new Square(); . This will create a Square container for me in which I can put stuff, for example other containers or just some values. If i pick the container up and play around with it, then the stuff inside is also moving, but what if I want to have some sort of counter for how many containers I created? I can't put this inside one container, since then other containers will depend on that container. So switching to java now..

Assume we want 3 containers, two squares and one sphere. I would do the following:

Square square1 = new Square();
Square square2 = new Square();
Sphere sphere1 = new Sphere();

Now if I want to add some value to the first square1 container, then I would assume it defines some possible values for me (for example a color) and I can set this color, for example:

square1.setColor("Red");

Now if we look at the definition of this Square type, then we need to have some variable to store this color. So A basic definition looks like:

class Square
{
    private String color;
    //Construct a Square container
    public Square()
    {
        this.color = "White"; //here we can set a default color
    }
   //This method will set the color of this container
   public void setColor(String color)
   {
       this.color = color;
   }
}

Now if we play around with this square1 object, then the color will move with it. The variable color (in the Square class) is said to be non-static, or a member variable of the Square class.
What does it mean if we make this variable static? Well this variable will then be shared over all the Square type containers! So if we modify this variable somewhere, it will modify for all! So normally you do not want that, and therefore you create new instances of an container (object) with the new keyword.
So if I say: new Square(), it really means, make a new container for me, initialize it with default values and variables which I can individualy set.
Ok, small summary:

Assume two objects:
Square square1 = new Square();
Square square2 = new Square();

and the Square class defines two variables:

public static String name;
public String color;

Now if we say:
square1.name = "Fiat";
square1.color = "red";

then the result is:
square1.name equals "Fiat"
square2.name equals "Fiat"
square1.color = "red";
square2.color = "white"; (white is the default value)

I hope it's clear now.. if not, search on google for static vs non-static for more examples and more explanation. Or of course, ask us. :-)
Mark
Mark,
That is fantastic! I have been trying to understand how it works, and you have made it very clear to me.
Your help has been invaluable.
Again many, many thanks.
John