Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

not sure if i'm going blind?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

i was testing something and can't see what i'm doing wrong with the code below. when i compile it, i get the msg that an identifier is expected in the line String [] a;. thxs.
 
rey leon
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, i had noticed that too (String [] a = new String [1]; compiles!).

Originally posted by Chandra Kota:
Even I'm confused with this. When we declare and Initialize the array, there is no problem. But when we separate declaration and initialization, we are having the problem. Can anyone explain whats wrong with that.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because declaring and initializing class member variables is one step process you can't initialize instance variables like this. The initialization

a=new String[2];

should be either inside a method or constructor because that type of initialization is only though methods..... Remember OOPS concepts
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we cant have any variable at class level without its type like

Class t{
int i;
i=10; // this will create a compile time error
It should be like this in one step int i=10;
}

Class b{ int j;}

Class c extends b { j=10; } even this will also create aproblem.

Thanks
 
rey leon
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both your help!
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a "point of view" and maybe even a flawed one..... but

when you separate the declaration from the initialization like that it must be done inside a method because you can't have code ( a = String[1] ) OUTSIDE of a method. Executable code must be in a method.....
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should go in either a constructor, a method, or an unnamed executable block surrounded by { }, which runs at instantiation time (or class loading if it's a static block).
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey
this is indeed a good question and answers with explanation.
excellent...
 
It's feeding time! Give me the food you were going to give to this tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic