Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors and Arrays  RSS feed

 
Cecil Phillip
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the compiler give me this error for my code
Test.java [19:1] illegal start of expression
tin = {1,2,3,4};
^
1 error
Errors compiling Maya.

public class Test {
private int[] tin;

public Test {
this.tin = {1,2,3,4};
}
}
 
Cecil Phillip
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction
Test.java [19:1] illegal start of expression
tin = {1,2,3,4};
^
1 error
Errors compiling Test.java

public class Test {
private int[] tin;
public Test {
this.tin = {1,2,3,4};
}
}
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I understand it, the {...} notation can only be used in the declaration of an array: int[] tin = {1,2,3,4};
If you later want to set tin to some other array of values, you will need to use an anonymous array: tin = new int[] {1,2,3,4};
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your constructor?
You are using the "this" command which calls one of your constructors in your class. If you define the array in your constructor, then you can call the array like you are doing. But I would just declare the entire array in the constructor or declare it in the main method.
HTH
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!