aspose file tools*
The moose likes Beginning Java and the fly likes class Temp1 { 	int d[]; 	int a[]={43,34,344}; 	d=a; }   what is the problem in the given code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class Temp1 { 	int d[]; 	int a[]={43,34,344}; 	d=a; }   what is the problem in the given code" Watch "class Temp1 { 	int d[]; 	int a[]={43,34,344}; 	d=a; }   what is the problem in the given code" New topic
Author

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

saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
class Temp1 { int d[]; int a[]={43,34,344}; d=a; } what is the problem in the given code
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you try to compile it ?


[My Blog]
All roads lead to JavaRanch
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
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)



Bye,
Nicola
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369

what do you think of d=a assignment statement: is it allowed in that context?


[List of FAQs] | [Android FAQ] | [Samuh Varta]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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.

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
thanks for giving me valuable suggestions.
Sean Collins
Greenhorn

Joined: Nov 16, 2009
Posts: 8
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

Joined: Apr 10, 2010
Posts: 308
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class Temp1 { int d[]; int a[]={43,34,344}; d=a; } what is the problem in the given code
 
Similar Threads
Reg:java.lang.ArrayIndexOutOfBoundsException:
Double to string giving exta values
javac and classpath doubt
Stack Over flow error when Instantiating
Array Question