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

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

 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Temp1 { int d[]; int a[]={43,34,344}; d=a; } what is the problem in the given code
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to compile it ?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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)

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you think of d=a assignment statement: is it allowed in that context?
 
salvin francis
Bartender
Posts: 1277
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks for giving me valuable suggestions.
 
Sean Collins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic