• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static initializer doesn't return value.

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why Static initializer doesn't return value.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jeff sir.i was wrong
 
reply
    Bookmark Topic Watch Topic
  • New Topic