• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error in Array Declartion inside class

 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hello,

In the above code, I am not able to figure out why it is throwing error on line 3. same declaration(line 3 & 4) doesn't throw any error inside method.
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the error say?
 
Vidya Shivram
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ERROR: “Syntax error on token ”;“, , expected” .

I know this error is because I am not initializing in the same line of declaration. But, it works fine inside method.
so what is the reason for this to not work here ?
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does not allow free floating code in a class other than fields (§8.3), methods (§8.4), classes (§8.5), and interfaces (§8.5).
A class body may also contain instance initializers (§8.6), static initializers (§8.7), and declarations of constructors (§8.8) for the class.

See this for details: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.6
 
Vidya Shivram
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. I am java beginner. Can you tell me what is considered free floating code in java(any criteria ?) or it would be helpful if you can share good doc to explain same.

Thanks again
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chivid Ram wrote:Thanks Paul. I am java beginner. Can you tell me what is considered free floating code in java(any criteria ?) or it would be helpful if you can share good doc to explain same.

Thanks again


Criteria is exactly what I quoted above. Anything else is not allowed. For example, line in your case does not fall under any of the allowed things.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chivid Ram wrote:In the above code, I am not able to figure out why it is throwing error on line 3. same declaration(line 3 & 4) doesn't throw any error inside method.


As mentioned in Paul's post, you are only allowed to have fields, constructors, methods, intializers (both static and instance), classes and interfaces in your class. All other code (like a=new int[3]; in your code snippet) is not allowed and will result in a compiler error.

Here are a few alternatives to initialize that array.You could also use a seperate method to create the array and use one of the above alternatives to initialize the array reference variable
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic