Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static initializer doesn't return value.

 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why Static initializer doesn't return value.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose that it did, where would that return value then go to?

What do you think is calling the static initializer (and when)?
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Suppose that it did, where would that return value then go to?

What do you think is calling the static initializer (and when)?



to initailize static state and at the time of loading the class.

and i also want to know where memory alocated for final variable and return value.
i think final variable in heap and return value in stack. am i write.please explain this also.
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh raushan wrote:to initailize static state and at the time of loading the class.

Fine, but that doesn't answer Jesper's question: what should it return, and where would it return it to?

and i also want to know where memory alocated for final variable and return value.
i think final variable in heap and return value in stack. am i write.please explain this also.

The best place to find out about all that stuff is the JLS

Java is a memory-managed language, so 99% of the time you create objects and let the JVM decide where they go and when they get removed. If this is just a mental exercise, by all means knock yourself out; but quite honestly, it won't help you to become a better programmer. In fact, it's more likely to side-track you from things that are more important.

Personally, I can't remember the last time I worried about where something goes (oops, tell a lie; it was my SCJP exam back in 2006).

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh raushan wrote:
i think final variable in heap and return value in stack. am i write.please explain this also.


All local variables always go on the stack, whether they are final or not.

All object and all their member variables, all class definitions, all constant pools, go on the heap. The only exception to this is that starting at some point in Java 6, the JVM spec apparently allows objects to be created on the stack of escape analysis can prove that they won't live beyond the method invocation.

But, as already pointed out, that knowledge is not generally useful to a Java developer. In 14 years of Java development, that knowledge has never once affected how I write my code.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is why you won’t find such information in the JLS. It is an implementation detail, and as the last two people say, you don’t actually need to know it. Anyway, it can differ from implementation to implementation.
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Which is why you won’t find such information in the JLS...

Oops. Quite right. Sorry Ritesh. It'll be in the JVM specification.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . It'll be in the JVM specification. . . .
It might not be there, either, if it is an implementation detail.
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It might not be there, either, if it is an implementation detail.

Well, it certainly defines the areas, but I have to admit, I couldn't be fagged to find out whether it specifies where specific things like return values are put. It feels like a "stacky" sort of thing, but I'm certainly no system programmer.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . I couldn't be fagged to find out whether it specifies where specific things like return values are put. . . .
Winston
Quite right, too. You have better things to think about. I need to know that sort of thing when I am writing my compiler in FORTH, but ordinary users of Java don’t need that info at all.
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Ritesh raushan wrote:
i think final variable in heap and return value in stack. am i write.please explain this also.


All local variables always go on the stack, whether they are final or not.

All object and all their member variables, all class definitions, all constant pools, go on the heap. The only exception to this is that starting at some point in Java 6, the JVM spec apparently allows objects to be created on the stack of escape analysis can prove that they won't live beyond the method invocation.

But, as already pointed out, that knowledge is not generally useful to a Java developer. In 14 years of Java development, that knowledge has never once affected how I write my code.


sorry to say.i think only instance variable go onto the heap and one pointer in heap that points method area where all information about class(all method) is stored.
and i want to know where final instance variable is stored and one heap created for one application mean if i create two object of same class then both object share heap or created two heap for two object.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implementation details.

How do you think you could create two objects? Would you really create two heaps?
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Implementation details.

How do you think you could create two objects? Would you really create two heaps?


no Cambell sir i just asked a question .
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh raushan wrote:sorry to say.i think only instance variable go onto the heap

You would mostly be wrong. The correct answer has been stated multiple times (implementation detail), and the most usual implementation has also already been described by Jeff.


Ritesh raushan wrote:one heap created for one application mean if i create two object of same class then both object share heap or created two heap for two object.

There is only one heap to think about. If you have two objects they have different locations inside the same heap. Even if they are two objects of the same type.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh raushan wrote:
Jeff Verdegan wrote:
Ritesh raushan wrote:
i think final variable in heap and return value in stack. am i write.please explain this also.


All local variables always go on the stack, whether they are final or not.

All object and all their member variables, all class definitions, all constant pools, go on the heap. The only exception to this is that starting at some point in Java 6, the JVM spec apparently allows objects to be created on the stack of escape analysis can prove that they won't live beyond the method invocation.

But, as already pointed out, that knowledge is not generally useful to a Java developer. In 14 years of Java development, that knowledge has never once affected how I write my code.


sorry to say.i think only instance variable go onto the heap and one pointer in heap that points method area where all information about class(all method) is stored.


According the the JVM spec, the method area "is logically part of the heap." It doesn't say anything about a pointer in the heap pointing to the method area. Unless they've changed it for Java 7. And if they did, as others have pointed out, it wouldn't matter to Java programmers. That stuff only matters if you're implementing a JVM.

and i want to know where final instance variable is stored


As already stated: All instance variables (non-static member variables), final or not, are stored on the heap. They are stored as part of their object. And all class variables (static member variables), final or not, are also stored on the heap, as part of the class definition in the method area. (Unless the JVM is using escape analysis and putting some objects on the stack. Then those objects' instance variables, final or no will be on the stack, or possibly in registers.)

And all local variables, final or not, are stored on the stack.

http://docs.oracle.com/javase/specs/jvms/se5.0/html/Overview.doc.html#1732

And again, none if this matters for how you write your Java code, though I do think it's useful to have at least a rough understanding of this stuff.

and one heap created for one application mean if i create two object of same class then both object share heap or created two heap for two object.


There's one heap for each JVM instance. So if your program creates 2 objects, then those 2 objects are inhabiting the same heap.
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jeff sir.i was wrong
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic