• Post Reply Bookmark Topic Watch Topic
  • New Topic

Byte [] as Declaring Member Variable!  RSS feed

 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I write small example of code as below:-

How can i write byte array[] same way like Static String abc write?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make the byte array a class (static) field in exactly the same way the String was declared.

Try it and compile it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7985
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us why you want to do this?
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
As Mike. J. Thompson wrote:-

You can make the byte array a class (static) field in exactly the same way the String was declared.
Try it and compile it.

I try it as below:-

I got error as below:-


As Stephan van Hulst asked:-
Can you tell us why you want to do this?

I have one while loop which runs lots of time & in that i have to use one byte array same way.
Due to loop runs lots of time, memory utilized by this byte array, so for saving memory, i have to declare byte array same way as Static String declare.
Can i get help?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski says it is always a mistake to have a static variable anywhere. I am pretty sure he is correct. Don't create static fields without a good reason. The fact that they won't compile if called from the main method is a very bad reason for making things static.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandar Khire wrote:
I got error as below:-



This error is *not* related to the variable being static. If you tried to assign the variable (as a local) in your previous example in this way, you would get the same error. The correct Java syntax for this is ...



Henry
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error you made is that the square brackets are part of the type, not the identifier. Java allows you to place them next to the identifier in the declaration, but you need to omit them on all further uses of the identifier.

For this reason it is best practice to place the square brackets immediately following the type declaration as follows:



I agree with others that making variables static is almost always a bad idea, especially when you're a beginner. Doing it just to make something compile isn't the right reason.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!