| Author |
String add null to begining
|
vikas mhatre
Greenhorn
Joined: Apr 24, 2010
Posts: 26
|
|
Hello, I have following problem
when I print value of name it gives output as nulltest. How it is? and suppose I want to only test as output what should I do?
Thanks in advance
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
You haven't initialised the name field, which retains its default value of null. So your catenation call is equivalent to creating a String "null" which you are immediately adding the text "test" to.
|
 |
vikas mhatre
Greenhorn
Joined: Apr 24, 2010
Posts: 26
|
|
|
Thanks Sir, But I don't want that null then how should I do that?
|
 |
Vinoth Kumar Kannan
Ranch Hand
Joined: Aug 19, 2009
Posts: 276
|
|
Initialize your String with something. Like,
|
OCPJP 6
|
 |
vikas mhatre
Greenhorn
Joined: Apr 24, 2010
Posts: 26
|
|
|
But I don't want to initialize that string, then in that case will it possible?
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
vikas mhatre wrote: I want to only test as output what should I do?
try this.......
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
without initializing the string.........
output is
test
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
why line no 16 is ging compile time error.....
can anybody cmment
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
shanky sohar wrote:why line no 16 is ging compile time error.....
can anybody cmment
Code must be in a block -- either an initializer, constructor, or method. The only code that is allowed, in a class, but not in a code block, are declarations.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9956
|
|
|
To take Henry's reply one step further...when exactly would that code run? It's not part of the main() method, it's not part of the test() method. It's not part of any constructor. It's just kind of hanging out somewhere, with nobody using it.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Gaurav Raje
Ranch Hand
Joined: Jul 23, 2010
Posts: 131
|
|
to take it further (and pretend i have profound knowledge )
Always remember, classes are abstract entities(not to be confused with abstract classes). They are not supposed to execute any code or have sideeffects. I am not sayint they dont, but they are not supposed to. So this is one of the reason, most of the executable statements are tried to be avoided.
A class is just like a class which you would attend in school. Makes no sense, has nothing in real life except a schedule attached on the college website. It becomes real only if at least one student attends it. If no one attends, it should be as if the class never existed. Just like a good college will make sure thie resources which were to be utilized by this class were given to other classes, a good VM will do exactly the same. In such cases, construction side effects will create problems
Theoretically speaking
Always remember, declarations are memoryless statements. Definitions are memory statements. Even though internally they are executables, from the outside perspective, they just assign values. They have no strict rule about when they are initialized. All that is guaranteed is that they will be initialized before you actually need to use them.
In order to guarantee this, java has stated in its specifications, when the class will be loaded(and thus when the static fields will be initialized)> But this is not what a good programmer should rely on. You should thank god that java has such a specification. There are some languages which have neither.
As a programmer,
1) all executables have to be enclosed inside method bodies.
2) if you perform complex logic for initializing fields, it makes sense to put them in constructors or static initializer blocks(for static fields)
All said and done... as a conflicting argument
1) Declarations do take up space albeit very small(computer just doesnt remember what names variables have or stuff)... but this is taken care of by the linker(or prolly JVM in this case... wil edit if some language guru tells me... bin a while since i did this)
2) Even declarations are executable statements... but only with respect to the compiler. Not for us. Again this is hidden away from us.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Gaurav Raje wrote: . . . A class is just like a class which you would attend in school. . . .
No, it isn't. The word "class" can mean "type or "sort", as well as "lesson".
|
 |
Gaurav Raje
Ranch Hand
Joined: Jul 23, 2010
Posts: 131
|
|
|
aye aye sherriff.... you know how to create analogies better... but i hope i attempted to make a valid point
|
 |
 |
|
|
subject: String add null to begining
|
|
|