-2

I'm a beginner developing in Java, and I have run into a bit of a problem referencing an object (from a different class) in a class.

This is the code I used to create the object, from the file "Neighborhoods.java".

public class Neighborhoods {

    // variables
    String name;
    int vertices;
    double[] latCoords;
    double[] longCoords;

    public Neighborhoods() {
        Neighborhoods fisherHill = new Neighborhoods();
        fisherHill.name = "Fisher Hill";
        fisherHill.vertices = 4;
        fisherHill.latCoords = new double[] {42.331672, 42.326342, 42.334464, 42.335733};
        fisherHill.longCoords = new double[] {-71.131277, -71.143036, -71.148615, -71.141062};
    }
}

I then tried to use the object I created, "fisherHill" (from the class Neighborhoods) in my main class when calling a function from another different class (called "inPolygon").

inPolygon.check(Neighborhoods.fisherHill.vertices);

But for some reason, I'm getting an error when I try to reference the fisherHill object, as it says it can't be found.

I know I'm making some dumb mistake here, but I'm not sure what it is. Sorry if I used the wrong terminology in describing the code. Any help or suggestions would be much appreciated.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Rowan K.
  • 471
  • 2
  • 6
  • 10
  • 3
    Why are you making recursive call in your constructor? That will at some point of time result in `StackOverflowError`. – Rohit Jain Jul 23 '13 at 18:54
  • `Neighborhoods fisherHill = new Neighborhoods();` Wrong. Use `this` instead (e.g. `this.name = "Fisher Hill";`). Please get a Java book. – m0skit0 Jul 23 '13 at 18:55
  • Ok thanks for the help. I'll rewrite this a bit more properly. – Rowan K. Jul 23 '13 at 18:56

4 Answers4

1

You have several things wrong in there:

Neighborhoods fisherHill = new Neighborhoods();

Why are you instantiating a new object of the same class inside the constructor? Your constructor is called because a new object of this class is already going to be created. This new object is referenced as this. This is the proper way to initialize your class fields for a new object:

this.name = "Fisher Hill";
this.vertices = 4;
this.latCoords = new double[] {42.331672, 42.326342, 42.334464, 42.335733};
this.longCoords = new double[] {-71.131277, -71.143036, -71.148615, -71.141062};

As you can see in the other answers, this can be ommited. I personally prefer to put it, it makes code more readable for me.

And

inPolygon.check(Neighborhoods.fisherHill.vertices);

There's no such static field Neighborhoods.fisherHill. Even if it was there, fisherHill.vertices cannot be accessed because it has default accessibility.

You should create a Neighborhoods object, keep reference to it and extract the vertices field through a getter:

final Neighborhoods n = new Neighborhoods();
final int numVertices = n.getVertices();
inPolygon.check(numVertices);

Add in Neighborhoods class a getter for vertices field:

public int getVertices() {
    return this.vertices;
}

I suggest you get a Java book because you're obviously lacking knowledge of basic Java and OOP.

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • Thank you for the reply. I rewrote the code so that it now cleanly and properly creates the object inside the class. But that I'm still stuck with being unable to access a variable of an object from a different class. If I create a getter method as you suggested, I'd have to explicitly create "numVertices" variables for every object, which would be inefficient. – Rowan K. Jul 23 '13 at 20:26
  • 1
    If `numVertices` is the same among all `Neighborhoods` objects then you can declare a private static constant `private static final int NUM_VERTICES = 4`. You can reference this inside any object of this class as `Neighborhoods.NUM_VERTICES` or simply `NUM_VERTICES`. If `numVertices` varies between objects, then there's no way than replicate it over each object. – m0skit0 Jul 23 '13 at 21:16
  • Also in your case you don't even need this value assuming you mean the size of the arrays. You can simply do `public int getVertices() { return this.latCoords.length; }`, which is less prone to bugs, e.g. if you change the array size but forget to change the constant. – m0skit0 Jul 23 '13 at 21:21
  • This case fits the scenario you mentioned above. numVertices is different for every object, therefore there's no real efficient way to do implement a getter method. I kind of solved this myself by just instantiating the object in the main class, and not the Neighborhoods one. I could then access all the variables. – Rowan K. Jul 23 '13 at 21:27
  • 1
    Actually what you did is what I meant by `final Neighborhoods n = new Neighborhoods(); final int numVertices = n.getVertices(); inPolygon.check(numVertices);`. Glad to have helped. – m0skit0 Jul 23 '13 at 21:51
0

You have a recursive call in your constructor.

Offending line:

Neighborhoods fisherHill = new Neighborhoods();
mach
  • 8,315
  • 3
  • 33
  • 51
0

It should probably be something like

public class Neighborhoods {

  // variables
  private final String name;
  private final int vertices;
  private final double[] latCoords;
  private final double[] longCoords;

  public Neighborhoods() {
    name = "Fisher Hill";
    vertices = 4;
    latCoords = new double[] {42.331672, 42.326342, 42.334464, 42.335733};
    longCoords = new double[] {-71.131277, -71.143036, -71.148615, -71.141062};
  }

  public int getVertices() {
     return vertices;
  }

  //and some more getters
}

and then in your other class call

inPolygon.check(new Neighborhoods().getVertices());
RNJ
  • 15,272
  • 18
  • 86
  • 131
0

To answer your questions:

inPolygon.check(Neighborhoods.fisherHill.vertices);

But for some reason, I'm getting an error when I try to reference the fisherHill object, as it says it can't be found.

Here you are making a static reference, fisherHill in this way will only be accessible if it a static member.

Also, dont create instance inside constructor.

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42