Reputation: 789
I am writing two constructors of a class Triangle which takes as parameters : a String , an integer and a double array
private double [] side = new double[3];
public Triangle() {
this("",0,side);
//Here I have a compile error says "Cannot refer to an instance field side while explicitly invoking a constructor"
}
public Triangle(String color, int opacity,double [] side) {
super(color, opacity);
this.side = side ;
}
in the main method i wanted to initialize the triangle but I was not able to do so till now ..
i tried these two ways but non of them worked
GeoShapes[1] = new Triangle( "Red" , 89 , {2,4,3} ) ;
GeoShapes[2] = new Triangle( "white", 68 , new double{5,6,3} );
note : I did try initialize an array then putting its reference in the third parameter and it works , but this not what I need
could any one help what should I write in the third parameter ?
Upvotes: 2
Views: 5116
Reputation: 213193
You have to use it like this:
geoShapes[1] = new Triangle("Red" , 89 , new double[] {2,4,3});
You can use the array initializers only at the point of declaration, or with array creation expression.
Another option is to use varargs
as parameter type:
public Triangle(String color, int opacity, double... side) {
super(color, opacity);
this.side = side ;
}
then you can create instance using:
geoShapes[1] = new Triangle("Red", 89 , 2, 4, 3);
Regarding the issue in your 0-arg constructor:
public Triangle() {
this("",0,side);
}
you're trying to pass the instance field side
to the parameterized constructor, which isn't valid, because side
hasn't been initialized yet. All the initialization are done after this()
or super()
invocation. You should create an array and pass it like you would do normally. So this would work:
public Triangle() {
this("", 0, new double[] {0, 0, 0});
}
And please follow proper Java naming conventions. Variable names start with lowercase alphabets.
Upvotes: 8
Reputation: 279880
In
private double [] side = new double[3];
public Test() {
this("",0,side);
//Here I have a compile error says "Cannot refer to an instance field side while explicitly invoking a constructor"
}
You can't use side
because the constructor hasn't really been called yet and therefore instance variables haven't been initialized. This is explained in the Java Language Specification
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
The bold part is referring to the this()
call.
One option is to pass a new array.
public Test() {
this("",0, new double[3]);
}
You don't need to initialize the side
field because both your constructors do it.
For the other beef, check the other answers.
Upvotes: 1
Reputation: 68715
You are missing the closing bracket in both the cases and also new
to create an array:
new Triangle( "Red" , 89 , {2,4,3};
should be
new Triangle( "Red" , 89 , new double[]{2,4,3});
Upvotes: 0