2

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.

4
  • docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented Jul 8, 2013 at 14:31
  • What about declaring double[] array outside of the constructor and then creating the object array = new double[length]; inside the constructor? Commented Jul 8, 2013 at 14:33
  • 2
    just as a marginal remark: You should not name your class Class Commented Jul 8, 2013 at 14:34
  • 3
    I believe you should read about object oriented programming in Java, these are really basics. Commented Jul 8, 2013 at 14:35

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
    }    
}

Comments

0

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;
}

Comments

0
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];

    }

}

Comments

-1

You can do it

public  class Test  {
 double[] array;

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


}

2 Comments

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 !
@TheNewIdiot Thankyou. Did'nt realize.

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.