0

i know that this question has been asked many types, but i am not getting trough the problem. So following. I have created a class that is creating array of 2 positions. The goal is to create point coordinates so i can generate several points later. Hier is my code

import java.util.Random;

public class Coor {



private static int[] coord;

public static int[] generate(){
    coord = new int[2];   
    return coord;
}

public static void printX(){

        System.out.println("X = " + coord[0] );

}
public static void printY(){

    System.out.println("Y = " + coord[1] );

}

public static int randomFill(){
    Random rand = new Random();
    int randomNum = rand.nextInt(99);
    return randomNum;
}

public static void main(String args[]) {

      generate();
        for(int i = 0; i < 2; i++){
            coord[i] = randomFill();
        }
        printX();
        printY();
    }


}

So, this is working perfect, but what I want is to create the points in another class and to use them there, but I have no idea how to achieve this. I am new to java, and I have almost understood some examples in the oracle docs, but can not implement it. Can you please help me a little? I just need one example class which is obtaining the coordinates of the points, after that I can extend it alone for my needs.

3
  • Why are you creating everything as static? That's probably not your intention. Commented Jan 26, 2015 at 19:07
  • Your problem is you try programing in Java without learn the Java language. Let's take a look here. Commented Jan 26, 2015 at 19:13
  • Almost every new java programer has this problem. It is a real shame that new programmers need to understand static before they can write there first program. Commented Jan 26, 2015 at 19:26

1 Answer 1

1

You should not make your data static and you should provide a public constructor see below.

public class Coord {

private int[] coord;

public Coord(int x, int y) {
    coord = new int[2];
    coord[0] = x;
    coord[1] = y;
}

public void printX(){
    System.out.println("X = " + coord[0] );
}

public void printY(){
    System.out.println("Y = " + coord[1] );
}

public static void main(String[] args) {
    Coord c1 = new Coord(10, 11);
    Coord c2 = new Coord(23, 14);
}
}
Sign up to request clarification or add additional context in comments.

4 Comments

It's not necessary to use an array in your Coord class, because it's the coordinates himself. Use only two properties (x, y).
I completely agree and I favored keeping the structure of the posters class unchanged so as to keep focus on the issue which was his problem with static members.
You're right ! But I think is essential to advice the programmer to make some tutorials especially on the Object Oriented Programming. Because it's possible he doesn't understand the two instanciations and doesn't know how to use the references to access the properties / methods. (sorry for my poor english).
So thank you for the advices. I will try to increase my java knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.