Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

How do I make a constructor to set the length of a global array?

I have already tried several ways to do it, none successful.

Example:

public Class{

    public Class(int length){
       double[] array = new double[length]; <- this is not global

       L = length;
    }

   int L;
   double[] array = new double[L]; <- this does not work
}

I need an array with a length determined by Constructor.

share|improve this question
    
    
What about declaring double[] array outside of the constructor and then creating the object array = new double[length]; inside the constructor? – hax0r_n_code Jul 8 '13 at 14:33
2  
just as a marginal remark: You should not name your class Class – Marco Forberg Jul 8 '13 at 14:34
2  
I believe you should read about object oriented programming in Java, these are really basics. – sp00m Jul 8 '13 at 14:35

I think it's as simple as this:

public class MyClass{
    double[] array;

    public MyClass(int length){
        array = new double[length];
    }
}

I've also made the code actually compile :) You were missing some keywords etc.

If you want to access length in your code, use array.length rather than storing it redundantly in a separate field.

Also calling your class Class is a bad choice, even as an example, because it clashes with java.lang.Class.

share|improve this answer
3  
+1 for the Remark on java.lang.Class – Marco Forberg Jul 8 '13 at 14:35

Declare the array as member variable. Then initialize it in the constructor.

public class A{    
    private double[] array;    
    public Class(int length){
        array = new double[length];  
        L = length;
    }    
}

You could initialize it in second way. But then you need to use a fixed length

public class A{    
    private double[] array = new double[100];  // use fixed length  
    public Class(int length){
        array = new double[length];   
        L = length;
    }    
}
share|improve this answer

I don't know what you are trying to achieve but why you don't simply do it this way:

public class Class{
    public Class(int length){
        this.array = new double[length]; // <- this is not global
    }
    double[] array;
}
share|improve this answer

You can do it

public  class Test  {
 double[] array;

      public Test  (int length){
          array = new double[length]; <- this is not global


}
share|improve this answer
    
Please correct the code , you have never defined a class here and also the choice of Class as classname is incorrect as it clashes with java.lang.Class ! – NINCOMPOOP Jul 8 '13 at 14:41
    
@TheNewIdiot Thankyou. Did'nt realize. – sᴜʀᴇsʜ ᴀᴛᴛᴀ Jul 8 '13 at 14:43
public class aClass{
    //define the variable name here, but wait to initialize it in the constructor
    public double[] array;

    public aClass(int length){
        array = new double[length];

    }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.