• 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

class Temp1 { int d[]; int a[]={43,34,344}; d=a; } what is the problem in the given code

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Temp1 { int d[]; int a[]={43,34,344}; d=a; } what is the problem in the given code
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to compile it ?
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to perform an operation outside an initialization block, a constructor or a method (3 places where operations can be performed)


You can fix the problem in this way ( i used an initialization block)

(i put a main method to demonstrate it works)

 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you think of d=a assignment statement: is it allowed in that context?
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
saurabh agr, welcome to coderanch

I would suggest choosing a better title next time you write a post in code ranch, adding code in the title itself does not make sense...




corresponding compile error:



also note that stating "what is the problem in the given code" is not productive.
 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for giving me valuable suggestions.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw Nicola's answer and wondered if I should check my Java coding habits. I often do:



I'm guessing the static is redundant, but I wonder what a professional code stylist would recommend for this situation?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sean,
i am not a professional code stylist
In the context of the problem arised in this thread it was not a matter of style, but to see if the code compiles or not.

The most important thing is that you always know what you are doing.

In your case, as far as i know, you can freely use static initializers and then you should not change your coding habits, but be sure to know that the members initialized in a static block are initialized the first time the class is loaded.

 
reply
    Bookmark Topic Watch Topic
  • New Topic