Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why these code cannot be compiled???

 
smart andy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why cannot i creat a String Array???

pubic class andy
{ String[] a = new String[3];
a[0]="aaaa"
a[1]="bbbb"
a[2]="cccc"
public static void main(String[] args){}
}
wait for you answer. ....
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for starters, those assignments aren't inside any method or (initialization) block, so I bet the compiler gave you a whole slew of error messages.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and there's that whole missing semicolons thing...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's not forget the "pubic class"
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why???
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy, what do you mean? You don;t understand what Michael, Dirk, and Jim posted?
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't understand why assignments can't be initialized outof method or (initialization) block
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
smart andy,

Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy. (Hint: please do not use any obviously fictitious names).
Thanks again and we hope to see you around the ranch!!
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but these code can also be compiled(without static),look:
public class andy{
{String[] a = new String[3];
a[0]="sfdgc";
a[1]="a";
a[2]="b";
}
public static void main(String[] args){}
}
[ April 10, 2002: Message edited by: andy lau ]
 
Charlie Sturman
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by andy lau:
i don't understand why assignments can't be initialized outof method or (initialization) block

The compiler puts all static and instance initializers of a class into one special method,the class initialization method.Only the jvm at runtime invokes this special initialization method.
I feel like I am aiming at a moving target
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic