This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Constructors and Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Constructors and Arrays" Watch "Constructors and Arrays" New topic
Author

Constructors and Arrays

Cecil Phillip
Ranch Hand

Joined: Nov 05, 2001
Posts: 40
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

Joined: Nov 05, 2001
Posts: 40
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

Joined: May 29, 2003
Posts: 1258
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};


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors and Arrays
 
Similar Threads
why can not print List's content?
three new tin stars
four new tin stars
three new tin stars
Unboxing to vararg