0

I had to write a program that creates credit card numbers. I wrote 3 classes, the main class, from where i can get the type and the length of the card (eg. Visa 15 digits), the luhn formula and one class where i create the card. From the main class i send the type and the length of the card to the last class. So i started the last class like this (its not all the code, only the first lines):

public class Cards extends Program {

private int[] x = new int[length];
private int[] k = new int[length];
private int length; 
private String type;

public Cards(int l, String name) {
    length = l;
    type = name;
}

which is clearly wrong (error at the compile) but the thing is that only my pc (where i also wrote these classes) can run it, without compile error and getting the correct results. I know where the problem is and the other 2 classes are correct but i want to know how is it possible to run at my pc without a problem.

Ps: I used acm package, my jdk is 1.8.0_31 and i wrote them on notepad++

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ironik
  • 11
  • 3
  • 2
    Are you sure you really compile it? You can be running already compiled code... – libik Mar 02 '15 at 23:39
  • Can you share all your code? both mentions of libik can be valid, but therefore we need to see all of your code – flp Mar 02 '15 at 23:44
  • You aren't running the code you've posted, since it doesn't compile. You are most likely running an earlier version of it, which didn't have any errors. – azurefrog Mar 02 '15 at 23:44
  • You are right the whole i was running an earlier version of it, thank you all very much for your help!!! – ironik Mar 02 '15 at 23:52

2 Answers2

0

I have the same version of the JDK, and class Card does not compile. You are probably executing against a previously compiled .class . Are you sure your code never was something like:

public class Cards extends Program {

    private int[] x;
    private int[] k;
    private int length; 
    private String type;

    public Cards(int l, String name) {
        length = l;
        x = new int[length] ;
        k = new int[length] ;
        type = name;
    }
}

I bet it was! Try again after removing any file Cards.class in your PC.

Mario Rossi
  • 7,651
  • 27
  • 37
0

In my compiler it gives "Cannot reference a field before it is defined".

You can define them as:

private int length; 
private int[] x = new int[length];
private int[] k = new int[length];

However, that code is not clear because length is assigned in the constructor.

Perhaps it is most clear to write:

public class Cards extends Program {
    private int[] x;
    private int[] k;
    private int length; 
    private String type;
    public Cards(int l, String name) {
        this.length = l;
        this.x = new int[length];
        this.y = new int[length];
        this.type = name;
    }
}
Javier
  • 12,100
  • 5
  • 46
  • 57